18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/* linux/drivers/hwmon/s3c-hwmon.c
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2005, 2008, 2009 Simtec Electronics
58c2ecf20Sopenharmony_ci *	http://armlinux.simtec.co.uk/
68c2ecf20Sopenharmony_ci *	Ben Dooks <ben@simtec.co.uk>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * S3C24XX/S3C64XX ADC hwmon support
98c2ecf20Sopenharmony_ci*/
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/io.h>
148c2ecf20Sopenharmony_ci#include <linux/init.h>
158c2ecf20Sopenharmony_ci#include <linux/err.h>
168c2ecf20Sopenharmony_ci#include <linux/clk.h>
178c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
188c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
218c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/soc/samsung/s3c-adc.h>
248c2ecf20Sopenharmony_ci#include <linux/platform_data/hwmon-s3c.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct s3c_hwmon_attr {
278c2ecf20Sopenharmony_ci	struct sensor_device_attribute	in;
288c2ecf20Sopenharmony_ci	struct sensor_device_attribute	label;
298c2ecf20Sopenharmony_ci	char				in_name[12];
308c2ecf20Sopenharmony_ci	char				label_name[12];
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/**
348c2ecf20Sopenharmony_ci * struct s3c_hwmon - ADC hwmon client information
358c2ecf20Sopenharmony_ci * @lock: Access lock to serialise the conversions.
368c2ecf20Sopenharmony_ci * @client: The client we registered with the S3C ADC core.
378c2ecf20Sopenharmony_ci * @hwmon_dev: The hwmon device we created.
388c2ecf20Sopenharmony_ci * @attr: The holders for the channel attributes.
398c2ecf20Sopenharmony_ci*/
408c2ecf20Sopenharmony_cistruct s3c_hwmon {
418c2ecf20Sopenharmony_ci	struct mutex		lock;
428c2ecf20Sopenharmony_ci	struct s3c_adc_client	*client;
438c2ecf20Sopenharmony_ci	struct device		*hwmon_dev;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	struct s3c_hwmon_attr	attrs[8];
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/**
498c2ecf20Sopenharmony_ci * s3c_hwmon_read_ch - read a value from a given adc channel.
508c2ecf20Sopenharmony_ci * @dev: The device.
518c2ecf20Sopenharmony_ci * @hwmon: Our state.
528c2ecf20Sopenharmony_ci * @channel: The channel we're reading from.
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * Read a value from the @channel with the proper locking and sleep until
558c2ecf20Sopenharmony_ci * either the read completes or we timeout awaiting the ADC core to get
568c2ecf20Sopenharmony_ci * back to us.
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_cistatic int s3c_hwmon_read_ch(struct device *dev,
598c2ecf20Sopenharmony_ci			     struct s3c_hwmon *hwmon, int channel)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	int ret;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	ret = mutex_lock_interruptible(&hwmon->lock);
648c2ecf20Sopenharmony_ci	if (ret < 0)
658c2ecf20Sopenharmony_ci		return ret;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	dev_dbg(dev, "reading channel %d\n", channel);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	ret = s3c_adc_read(hwmon->client, channel);
708c2ecf20Sopenharmony_ci	mutex_unlock(&hwmon->lock);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	return ret;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#ifdef CONFIG_SENSORS_S3C_RAW
768c2ecf20Sopenharmony_ci/**
778c2ecf20Sopenharmony_ci * s3c_hwmon_show_raw - show a conversion from the raw channel number.
788c2ecf20Sopenharmony_ci * @dev: The device that the attribute belongs to.
798c2ecf20Sopenharmony_ci * @attr: The attribute being read.
808c2ecf20Sopenharmony_ci * @buf: The result buffer.
818c2ecf20Sopenharmony_ci *
828c2ecf20Sopenharmony_ci * This show deals with the raw attribute, registered for each possible
838c2ecf20Sopenharmony_ci * ADC channel. This does a conversion and returns the raw (un-scaled)
848c2ecf20Sopenharmony_ci * value returned from the hardware.
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_cistatic ssize_t s3c_hwmon_show_raw(struct device *dev,
878c2ecf20Sopenharmony_ci				  struct device_attribute *attr, char *buf)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct s3c_hwmon *adc = dev_get_drvdata(dev);
908c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sa = to_sensor_dev_attr(attr);
918c2ecf20Sopenharmony_ci	int ret;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	ret = s3c_hwmon_read_ch(dev, adc, sa->index);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	return  (ret < 0) ? ret : snprintf(buf, PAGE_SIZE, "%d\n", ret);
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(adc0_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 0);
998c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(adc1_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 1);
1008c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(adc2_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 2);
1018c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(adc3_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 3);
1028c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(adc4_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 4);
1038c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(adc5_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 5);
1048c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(adc6_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 6);
1058c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(adc7_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 7);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic struct attribute *s3c_hwmon_attrs[9] = {
1088c2ecf20Sopenharmony_ci	&sensor_dev_attr_adc0_raw.dev_attr.attr,
1098c2ecf20Sopenharmony_ci	&sensor_dev_attr_adc1_raw.dev_attr.attr,
1108c2ecf20Sopenharmony_ci	&sensor_dev_attr_adc2_raw.dev_attr.attr,
1118c2ecf20Sopenharmony_ci	&sensor_dev_attr_adc3_raw.dev_attr.attr,
1128c2ecf20Sopenharmony_ci	&sensor_dev_attr_adc4_raw.dev_attr.attr,
1138c2ecf20Sopenharmony_ci	&sensor_dev_attr_adc5_raw.dev_attr.attr,
1148c2ecf20Sopenharmony_ci	&sensor_dev_attr_adc6_raw.dev_attr.attr,
1158c2ecf20Sopenharmony_ci	&sensor_dev_attr_adc7_raw.dev_attr.attr,
1168c2ecf20Sopenharmony_ci	NULL,
1178c2ecf20Sopenharmony_ci};
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic struct attribute_group s3c_hwmon_attrgroup = {
1208c2ecf20Sopenharmony_ci	.attrs	= s3c_hwmon_attrs,
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic inline int s3c_hwmon_add_raw(struct device *dev)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	return sysfs_create_group(&dev->kobj, &s3c_hwmon_attrgroup);
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic inline void s3c_hwmon_remove_raw(struct device *dev)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	sysfs_remove_group(&dev->kobj, &s3c_hwmon_attrgroup);
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci#else
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic inline int s3c_hwmon_add_raw(struct device *dev) { return 0; }
1368c2ecf20Sopenharmony_cistatic inline void s3c_hwmon_remove_raw(struct device *dev) { }
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci#endif /* CONFIG_SENSORS_S3C_RAW */
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci/**
1418c2ecf20Sopenharmony_ci * s3c_hwmon_ch_show - show value of a given channel
1428c2ecf20Sopenharmony_ci * @dev: The device that the attribute belongs to.
1438c2ecf20Sopenharmony_ci * @attr: The attribute being read.
1448c2ecf20Sopenharmony_ci * @buf: The result buffer.
1458c2ecf20Sopenharmony_ci *
1468c2ecf20Sopenharmony_ci * Read a value from the ADC and scale it before returning it to the
1478c2ecf20Sopenharmony_ci * caller. The scale factor is gained from the channel configuration
1488c2ecf20Sopenharmony_ci * passed via the platform data when the device was registered.
1498c2ecf20Sopenharmony_ci */
1508c2ecf20Sopenharmony_cistatic ssize_t s3c_hwmon_ch_show(struct device *dev,
1518c2ecf20Sopenharmony_ci				 struct device_attribute *attr,
1528c2ecf20Sopenharmony_ci				 char *buf)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
1558c2ecf20Sopenharmony_ci	struct s3c_hwmon *hwmon = dev_get_drvdata(dev);
1568c2ecf20Sopenharmony_ci	struct s3c_hwmon_pdata *pdata = dev_get_platdata(dev);
1578c2ecf20Sopenharmony_ci	struct s3c_hwmon_chcfg *cfg;
1588c2ecf20Sopenharmony_ci	int ret;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	cfg = pdata->in[sen_attr->index];
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	ret = s3c_hwmon_read_ch(dev, hwmon, sen_attr->index);
1638c2ecf20Sopenharmony_ci	if (ret < 0)
1648c2ecf20Sopenharmony_ci		return ret;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	ret *= cfg->mult;
1678c2ecf20Sopenharmony_ci	ret = DIV_ROUND_CLOSEST(ret, cfg->div);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%d\n", ret);
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci/**
1738c2ecf20Sopenharmony_ci * s3c_hwmon_label_show - show label name of the given channel.
1748c2ecf20Sopenharmony_ci * @dev: The device that the attribute belongs to.
1758c2ecf20Sopenharmony_ci * @attr: The attribute being read.
1768c2ecf20Sopenharmony_ci * @buf: The result buffer.
1778c2ecf20Sopenharmony_ci *
1788c2ecf20Sopenharmony_ci * Return the label name of a given channel
1798c2ecf20Sopenharmony_ci */
1808c2ecf20Sopenharmony_cistatic ssize_t s3c_hwmon_label_show(struct device *dev,
1818c2ecf20Sopenharmony_ci				    struct device_attribute *attr,
1828c2ecf20Sopenharmony_ci				    char *buf)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
1858c2ecf20Sopenharmony_ci	struct s3c_hwmon_pdata *pdata = dev_get_platdata(dev);
1868c2ecf20Sopenharmony_ci	struct s3c_hwmon_chcfg *cfg;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	cfg = pdata->in[sen_attr->index];
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%s\n", cfg->name);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/**
1948c2ecf20Sopenharmony_ci * s3c_hwmon_create_attr - create hwmon attribute for given channel.
1958c2ecf20Sopenharmony_ci * @dev: The device to create the attribute on.
1968c2ecf20Sopenharmony_ci * @cfg: The channel configuration passed from the platform data.
1978c2ecf20Sopenharmony_ci * @channel: The ADC channel number to process.
1988c2ecf20Sopenharmony_ci *
1998c2ecf20Sopenharmony_ci * Create the scaled attribute for use with hwmon from the specified
2008c2ecf20Sopenharmony_ci * platform data in @pdata. The sysfs entry is handled by the routine
2018c2ecf20Sopenharmony_ci * s3c_hwmon_ch_show().
2028c2ecf20Sopenharmony_ci *
2038c2ecf20Sopenharmony_ci * The attribute name is taken from the configuration data if present
2048c2ecf20Sopenharmony_ci * otherwise the name is taken by concatenating in_ with the channel
2058c2ecf20Sopenharmony_ci * number.
2068c2ecf20Sopenharmony_ci */
2078c2ecf20Sopenharmony_cistatic int s3c_hwmon_create_attr(struct device *dev,
2088c2ecf20Sopenharmony_ci				 struct s3c_hwmon_chcfg *cfg,
2098c2ecf20Sopenharmony_ci				 struct s3c_hwmon_attr *attrs,
2108c2ecf20Sopenharmony_ci				 int channel)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr;
2138c2ecf20Sopenharmony_ci	int ret;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	snprintf(attrs->in_name, sizeof(attrs->in_name), "in%d_input", channel);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	attr = &attrs->in;
2188c2ecf20Sopenharmony_ci	attr->index = channel;
2198c2ecf20Sopenharmony_ci	sysfs_attr_init(&attr->dev_attr.attr);
2208c2ecf20Sopenharmony_ci	attr->dev_attr.attr.name  = attrs->in_name;
2218c2ecf20Sopenharmony_ci	attr->dev_attr.attr.mode  = S_IRUGO;
2228c2ecf20Sopenharmony_ci	attr->dev_attr.show = s3c_hwmon_ch_show;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	ret =  device_create_file(dev, &attr->dev_attr);
2258c2ecf20Sopenharmony_ci	if (ret < 0) {
2268c2ecf20Sopenharmony_ci		dev_err(dev, "failed to create input attribute\n");
2278c2ecf20Sopenharmony_ci		return ret;
2288c2ecf20Sopenharmony_ci	}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/* if this has a name, add a label */
2318c2ecf20Sopenharmony_ci	if (cfg->name) {
2328c2ecf20Sopenharmony_ci		snprintf(attrs->label_name, sizeof(attrs->label_name),
2338c2ecf20Sopenharmony_ci			 "in%d_label", channel);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci		attr = &attrs->label;
2368c2ecf20Sopenharmony_ci		attr->index = channel;
2378c2ecf20Sopenharmony_ci		sysfs_attr_init(&attr->dev_attr.attr);
2388c2ecf20Sopenharmony_ci		attr->dev_attr.attr.name  = attrs->label_name;
2398c2ecf20Sopenharmony_ci		attr->dev_attr.attr.mode  = S_IRUGO;
2408c2ecf20Sopenharmony_ci		attr->dev_attr.show = s3c_hwmon_label_show;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci		ret = device_create_file(dev, &attr->dev_attr);
2438c2ecf20Sopenharmony_ci		if (ret < 0) {
2448c2ecf20Sopenharmony_ci			device_remove_file(dev, &attrs->in.dev_attr);
2458c2ecf20Sopenharmony_ci			dev_err(dev, "failed to create label attribute\n");
2468c2ecf20Sopenharmony_ci		}
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	return ret;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic void s3c_hwmon_remove_attr(struct device *dev,
2538c2ecf20Sopenharmony_ci				  struct s3c_hwmon_attr *attrs)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	device_remove_file(dev, &attrs->in.dev_attr);
2568c2ecf20Sopenharmony_ci	device_remove_file(dev, &attrs->label.dev_attr);
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci/**
2608c2ecf20Sopenharmony_ci * s3c_hwmon_probe - device probe entry.
2618c2ecf20Sopenharmony_ci * @dev: The device being probed.
2628c2ecf20Sopenharmony_ci*/
2638c2ecf20Sopenharmony_cistatic int s3c_hwmon_probe(struct platform_device *dev)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	struct s3c_hwmon_pdata *pdata = dev_get_platdata(&dev->dev);
2668c2ecf20Sopenharmony_ci	struct s3c_hwmon *hwmon;
2678c2ecf20Sopenharmony_ci	int ret = 0;
2688c2ecf20Sopenharmony_ci	int i;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	if (!pdata) {
2718c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "no platform data supplied\n");
2728c2ecf20Sopenharmony_ci		return -EINVAL;
2738c2ecf20Sopenharmony_ci	}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	hwmon = devm_kzalloc(&dev->dev, sizeof(struct s3c_hwmon), GFP_KERNEL);
2768c2ecf20Sopenharmony_ci	if (hwmon == NULL)
2778c2ecf20Sopenharmony_ci		return -ENOMEM;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	platform_set_drvdata(dev, hwmon);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	mutex_init(&hwmon->lock);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	/* Register with the core ADC driver. */
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	hwmon->client = s3c_adc_register(dev, NULL, NULL, 0);
2868c2ecf20Sopenharmony_ci	if (IS_ERR(hwmon->client)) {
2878c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "cannot register adc\n");
2888c2ecf20Sopenharmony_ci		return PTR_ERR(hwmon->client);
2898c2ecf20Sopenharmony_ci	}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	/* add attributes for our adc devices. */
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	ret = s3c_hwmon_add_raw(&dev->dev);
2948c2ecf20Sopenharmony_ci	if (ret)
2958c2ecf20Sopenharmony_ci		goto err_registered;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	/* register with the hwmon core */
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	hwmon->hwmon_dev = hwmon_device_register(&dev->dev);
3008c2ecf20Sopenharmony_ci	if (IS_ERR(hwmon->hwmon_dev)) {
3018c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "error registering with hwmon\n");
3028c2ecf20Sopenharmony_ci		ret = PTR_ERR(hwmon->hwmon_dev);
3038c2ecf20Sopenharmony_ci		goto err_raw_attribute;
3048c2ecf20Sopenharmony_ci	}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(pdata->in); i++) {
3078c2ecf20Sopenharmony_ci		struct s3c_hwmon_chcfg *cfg = pdata->in[i];
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci		if (!cfg)
3108c2ecf20Sopenharmony_ci			continue;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci		if (cfg->mult >= 0x10000)
3138c2ecf20Sopenharmony_ci			dev_warn(&dev->dev,
3148c2ecf20Sopenharmony_ci				 "channel %d multiplier too large\n",
3158c2ecf20Sopenharmony_ci				 i);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci		if (cfg->div == 0) {
3188c2ecf20Sopenharmony_ci			dev_err(&dev->dev, "channel %d divider zero\n", i);
3198c2ecf20Sopenharmony_ci			continue;
3208c2ecf20Sopenharmony_ci		}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci		ret = s3c_hwmon_create_attr(&dev->dev, pdata->in[i],
3238c2ecf20Sopenharmony_ci					    &hwmon->attrs[i], i);
3248c2ecf20Sopenharmony_ci		if (ret) {
3258c2ecf20Sopenharmony_ci			dev_err(&dev->dev,
3268c2ecf20Sopenharmony_ci					"error creating channel %d\n", i);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci			for (i--; i >= 0; i--)
3298c2ecf20Sopenharmony_ci				s3c_hwmon_remove_attr(&dev->dev,
3308c2ecf20Sopenharmony_ci							      &hwmon->attrs[i]);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci			goto err_hwmon_register;
3338c2ecf20Sopenharmony_ci		}
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	return 0;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci err_hwmon_register:
3398c2ecf20Sopenharmony_ci	hwmon_device_unregister(hwmon->hwmon_dev);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci err_raw_attribute:
3428c2ecf20Sopenharmony_ci	s3c_hwmon_remove_raw(&dev->dev);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci err_registered:
3458c2ecf20Sopenharmony_ci	s3c_adc_release(hwmon->client);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	return ret;
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic int s3c_hwmon_remove(struct platform_device *dev)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	struct s3c_hwmon *hwmon = platform_get_drvdata(dev);
3538c2ecf20Sopenharmony_ci	int i;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	s3c_hwmon_remove_raw(&dev->dev);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(hwmon->attrs); i++)
3588c2ecf20Sopenharmony_ci		s3c_hwmon_remove_attr(&dev->dev, &hwmon->attrs[i]);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	hwmon_device_unregister(hwmon->hwmon_dev);
3618c2ecf20Sopenharmony_ci	s3c_adc_release(hwmon->client);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	return 0;
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic struct platform_driver s3c_hwmon_driver = {
3678c2ecf20Sopenharmony_ci	.driver	= {
3688c2ecf20Sopenharmony_ci		.name		= "s3c-hwmon",
3698c2ecf20Sopenharmony_ci	},
3708c2ecf20Sopenharmony_ci	.probe		= s3c_hwmon_probe,
3718c2ecf20Sopenharmony_ci	.remove		= s3c_hwmon_remove,
3728c2ecf20Sopenharmony_ci};
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cimodule_platform_driver(s3c_hwmon_driver);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3778c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("S3C ADC HWMon driver");
3788c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
3798c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:s3c-hwmon");
380