mm-only debug patch...
[mmotm.git] / drivers / hwmon / mc13783-adc.c
blobae53a7d94c4e965e9b8dfeab0192ce2467e9f44e
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-private.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/init.h>
28 #include <linux/err.h>
30 #define MC13783_ADC_NAME "mc13783-adc"
32 struct mc13783_adc_priv {
33 struct mc13783 *mc13783;
34 struct device *hwmon_dev;
37 static ssize_t mc13783_adc_show_name(struct device *dev, struct device_attribute
38 *devattr, char *buf)
40 return sprintf(buf, "mc13783_adc\n");
43 static unsigned int mc13783_adc_read(struct device *dev,
44 struct device_attribute *devattr)
46 struct platform_device *pdev = to_platform_device(dev);
47 struct mc13783_adc_priv *priv = platform_get_drvdata(pdev);
48 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
49 unsigned int channel = attr->index;
50 unsigned int sample[4];
52 mc13783_adc_do_conversion(priv->mc13783, MC13783_ADC_MODE_MULT_CHAN,
53 channel, sample);
55 channel &= 0x7;
57 return (sample[channel % 4] >> (channel > 3 ? 14 : 2)) & 0x3ff;
60 static ssize_t mc13783_adc_read_bp(struct device *dev,
61 struct device_attribute *devattr, char *buf)
63 unsigned res = mc13783_adc_read(dev, devattr);
66 * BP (channel 2) reports with offset 2.4V to the actual value to fit
67 * the input range of the ADC. unit = 2.25mV = 9/4 mV.
69 res = DIV_ROUND_CLOSEST(res * 9, 4) + 2400;
71 return sprintf(buf, "%u\n", res);
74 static ssize_t mc13783_adc_read_gp(struct device *dev,
75 struct device_attribute *devattr, char *buf)
77 unsigned res = mc13783_adc_read(dev, devattr);
80 * input range is [0, 2.3V], res has 10 bits, so each bit
81 * is worth 9/4 mV.
83 res = DIV_ROUND_CLOSEST(res * 9, 4);
85 return sprintf(buf, "%u\n", res);
88 static DEVICE_ATTR(name, S_IRUGO, mc13783_adc_show_name, NULL);
89 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, mc13783_adc_read_bp, NULL, 2);
90 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, mc13783_adc_read_gp, NULL, 5);
91 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, mc13783_adc_read_gp, NULL, 6);
92 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, mc13783_adc_read_gp, NULL, 7);
93 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, mc13783_adc_read_gp, NULL, 8);
94 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, mc13783_adc_read_gp, NULL, 9);
95 static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, mc13783_adc_read_gp, NULL, 10);
96 static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, mc13783_adc_read_gp, NULL, 11);
97 static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, mc13783_adc_read_gp, NULL, 12);
98 static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, mc13783_adc_read_gp, NULL, 13);
99 static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, mc13783_adc_read_gp, NULL, 14);
100 static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, mc13783_adc_read_gp, NULL, 15);
102 static struct attribute *mc13783_attr[] =
104 &dev_attr_name.attr,
105 &sensor_dev_attr_in2_input.dev_attr.attr,
106 &sensor_dev_attr_in5_input.dev_attr.attr,
107 &sensor_dev_attr_in6_input.dev_attr.attr,
108 &sensor_dev_attr_in7_input.dev_attr.attr,
109 &sensor_dev_attr_in8_input.dev_attr.attr,
110 &sensor_dev_attr_in9_input.dev_attr.attr,
111 &sensor_dev_attr_in10_input.dev_attr.attr,
112 &sensor_dev_attr_in11_input.dev_attr.attr,
113 NULL
116 static const struct attribute_group mc13783_group = {
117 .attrs = mc13783_attr,
120 /* last four channels may be occupied by the touchscreen */
121 static struct attribute *mc13783_attr_ts[] =
123 &sensor_dev_attr_in12_input.dev_attr.attr,
124 &sensor_dev_attr_in13_input.dev_attr.attr,
125 &sensor_dev_attr_in14_input.dev_attr.attr,
126 &sensor_dev_attr_in15_input.dev_attr.attr,
127 NULL
130 static const struct attribute_group mc13783_group_ts = {
131 .attrs = mc13783_attr_ts,
134 static int __init mc13783_adc_probe(struct platform_device *pdev)
136 struct mc13783_adc_priv *priv;
137 int ret;
139 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
140 if (!priv)
141 return -ENOMEM;
143 priv->mc13783 = dev_get_drvdata(pdev->dev.parent);
145 platform_set_drvdata(pdev, priv);
147 /* Register sysfs hooks */
148 ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group);
149 if (ret)
150 goto out_err_create1;
152 if (!(priv->mc13783->flags & MC13783_USE_TOUCHSCREEN))
153 ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group_ts);
154 if (ret)
155 goto out_err_create2;
157 priv->hwmon_dev = hwmon_device_register(&pdev->dev);
158 if (IS_ERR(priv->hwmon_dev)) {
159 ret = PTR_ERR(priv->hwmon_dev);
160 dev_err(&pdev->dev,
161 "hwmon_device_register failed with %d.\n", ret);
162 goto out_err_register;
166 return 0;
168 out_err_register:
170 if (!(priv->mc13783->flags & MC13783_USE_TOUCHSCREEN))
171 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts);
172 out_err_create2:
174 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group);
175 out_err_create1:
177 platform_set_drvdata(pdev, NULL);
178 kfree(priv);
180 return ret;
183 static int __devexit mc13783_adc_remove(struct platform_device *pdev)
185 struct mc13783_adc_priv *priv = platform_get_drvdata(pdev);
187 hwmon_device_unregister(&pdev->dev);
189 if (!(priv->mc13783->flags & MC13783_USE_TOUCHSCREEN))
190 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts);
192 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group);
194 platform_set_drvdata(pdev, NULL);
195 kfree(priv);
197 return 0;
200 static struct platform_driver mc13783_adc_driver = {
201 .remove = __devexit_p(mc13783_adc_remove),
202 .driver = {
203 .owner = THIS_MODULE,
204 .name = MC13783_ADC_NAME,
208 static int __init mc13783_adc_init(void)
210 return platform_driver_probe(&mc13783_adc_driver, mc13783_adc_probe);
213 static void __exit mc13783_adc_exit(void)
215 platform_driver_unregister(&mc13783_adc_driver);
218 module_init(mc13783_adc_init);
219 module_exit(mc13783_adc_exit);
221 MODULE_DESCRIPTION("MC13783 ADC driver");
222 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
223 MODULE_LICENSE("GPL");
224 MODULE_ALIAS("platform:" MC13783_ADC_NAME);