hwmon/f71882fg: Set platform drvdata to NULL later
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / mc13783-adc.c
blobd5226c9e1201521046458a403d83fba245032db3
1 /*
2 * Driver for the Freescale Semiconductor MC13783 adc.
4 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
5 * Copyright (C) 2009 Sascha Hauer, Pengutronix
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <linux/mfd/mc13783.h>
22 #include <linux/platform_device.h>
23 #include <linux/hwmon-sysfs.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/hwmon.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/err.h>
31 #define MC13783_ADC_NAME "mc13783-adc"
33 struct mc13783_adc_priv {
34 struct mc13783 *mc13783;
35 struct device *hwmon_dev;
38 static ssize_t mc13783_adc_show_name(struct device *dev, struct device_attribute
39 *devattr, char *buf)
41 return sprintf(buf, "mc13783_adc\n");
44 static int mc13783_adc_read(struct device *dev,
45 struct device_attribute *devattr, unsigned int *val)
47 struct platform_device *pdev = to_platform_device(dev);
48 struct mc13783_adc_priv *priv = platform_get_drvdata(pdev);
49 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
50 unsigned int channel = attr->index;
51 unsigned int sample[4];
52 int ret;
54 ret = mc13783_adc_do_conversion(priv->mc13783,
55 MC13783_ADC_MODE_MULT_CHAN,
56 channel, sample);
57 if (ret)
58 return ret;
60 channel &= 0x7;
62 *val = (sample[channel % 4] >> (channel > 3 ? 14 : 2)) & 0x3ff;
64 return 0;
67 static ssize_t mc13783_adc_read_bp(struct device *dev,
68 struct device_attribute *devattr, char *buf)
70 unsigned val;
71 int ret = mc13783_adc_read(dev, devattr, &val);
73 if (ret)
74 return ret;
77 * BP (channel 2) reports with offset 2.4V to the actual value to fit
78 * the input range of the ADC. unit = 2.25mV = 9/4 mV.
80 val = DIV_ROUND_CLOSEST(val * 9, 4) + 2400;
82 return sprintf(buf, "%u\n", val);
85 static ssize_t mc13783_adc_read_gp(struct device *dev,
86 struct device_attribute *devattr, char *buf)
88 unsigned val;
89 int ret = mc13783_adc_read(dev, devattr, &val);
91 if (ret)
92 return ret;
95 * input range is [0, 2.3V], val has 10 bits, so each bit
96 * is worth 9/4 mV.
98 val = DIV_ROUND_CLOSEST(val * 9, 4);
100 return sprintf(buf, "%u\n", val);
103 static DEVICE_ATTR(name, S_IRUGO, mc13783_adc_show_name, NULL);
104 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, mc13783_adc_read_bp, NULL, 2);
105 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, mc13783_adc_read_gp, NULL, 5);
106 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, mc13783_adc_read_gp, NULL, 6);
107 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, mc13783_adc_read_gp, NULL, 7);
108 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, mc13783_adc_read_gp, NULL, 8);
109 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, mc13783_adc_read_gp, NULL, 9);
110 static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, mc13783_adc_read_gp, NULL, 10);
111 static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, mc13783_adc_read_gp, NULL, 11);
112 static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, mc13783_adc_read_gp, NULL, 12);
113 static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, mc13783_adc_read_gp, NULL, 13);
114 static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, mc13783_adc_read_gp, NULL, 14);
115 static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, mc13783_adc_read_gp, NULL, 15);
117 static struct attribute *mc13783_attr[] = {
118 &dev_attr_name.attr,
119 &sensor_dev_attr_in2_input.dev_attr.attr,
120 &sensor_dev_attr_in5_input.dev_attr.attr,
121 &sensor_dev_attr_in6_input.dev_attr.attr,
122 &sensor_dev_attr_in7_input.dev_attr.attr,
123 &sensor_dev_attr_in8_input.dev_attr.attr,
124 &sensor_dev_attr_in9_input.dev_attr.attr,
125 &sensor_dev_attr_in10_input.dev_attr.attr,
126 &sensor_dev_attr_in11_input.dev_attr.attr,
127 NULL
130 static const struct attribute_group mc13783_group = {
131 .attrs = mc13783_attr,
134 /* last four channels may be occupied by the touchscreen */
135 static struct attribute *mc13783_attr_ts[] = {
136 &sensor_dev_attr_in12_input.dev_attr.attr,
137 &sensor_dev_attr_in13_input.dev_attr.attr,
138 &sensor_dev_attr_in14_input.dev_attr.attr,
139 &sensor_dev_attr_in15_input.dev_attr.attr,
140 NULL
143 static const struct attribute_group mc13783_group_ts = {
144 .attrs = mc13783_attr_ts,
147 static int mc13783_adc_use_touchscreen(struct platform_device *pdev)
149 struct mc13783_adc_priv *priv = platform_get_drvdata(pdev);
150 unsigned flags = mc13783_get_flags(priv->mc13783);
152 return flags & MC13783_USE_TOUCHSCREEN;
155 static int __init mc13783_adc_probe(struct platform_device *pdev)
157 struct mc13783_adc_priv *priv;
158 int ret;
160 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
161 if (!priv)
162 return -ENOMEM;
164 priv->mc13783 = dev_get_drvdata(pdev->dev.parent);
166 platform_set_drvdata(pdev, priv);
168 /* Register sysfs hooks */
169 ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group);
170 if (ret)
171 goto out_err_create1;
173 if (!mc13783_adc_use_touchscreen(pdev)) {
174 ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group_ts);
175 if (ret)
176 goto out_err_create2;
179 priv->hwmon_dev = hwmon_device_register(&pdev->dev);
180 if (IS_ERR(priv->hwmon_dev)) {
181 ret = PTR_ERR(priv->hwmon_dev);
182 dev_err(&pdev->dev,
183 "hwmon_device_register failed with %d.\n", ret);
184 goto out_err_register;
188 return 0;
190 out_err_register:
192 if (!mc13783_adc_use_touchscreen(pdev))
193 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts);
194 out_err_create2:
196 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group);
197 out_err_create1:
199 platform_set_drvdata(pdev, NULL);
200 kfree(priv);
202 return ret;
205 static int __devexit mc13783_adc_remove(struct platform_device *pdev)
207 struct mc13783_adc_priv *priv = platform_get_drvdata(pdev);
209 hwmon_device_unregister(priv->hwmon_dev);
211 if (!mc13783_adc_use_touchscreen(pdev))
212 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts);
214 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group);
216 platform_set_drvdata(pdev, NULL);
217 kfree(priv);
219 return 0;
222 static struct platform_driver mc13783_adc_driver = {
223 .remove = __devexit_p(mc13783_adc_remove),
224 .driver = {
225 .owner = THIS_MODULE,
226 .name = MC13783_ADC_NAME,
230 static int __init mc13783_adc_init(void)
232 return platform_driver_probe(&mc13783_adc_driver, mc13783_adc_probe);
235 static void __exit mc13783_adc_exit(void)
237 platform_driver_unregister(&mc13783_adc_driver);
240 module_init(mc13783_adc_init);
241 module_exit(mc13783_adc_exit);
243 MODULE_DESCRIPTION("MC13783 ADC driver");
244 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
245 MODULE_LICENSE("GPL");
246 MODULE_ALIAS("platform:" MC13783_ADC_NAME);