]>
Commit | Line | Data |
---|---|---|
93ee0a75 | 1 | /* |
0c273215 | 2 | * Driver for the ADC on Freescale Semiconductor MC13783 and MC13892 PMICs. |
93ee0a75 LF |
3 | * |
4 | * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved. | |
5 | * Copyright (C) 2009 Sascha Hauer, Pengutronix | |
6 | * | |
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. | |
15 | * | |
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 | |
19 | */ | |
20 | ||
0c273215 | 21 | #include <linux/mfd/mc13xxx.h> |
93ee0a75 LF |
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> | |
5a0e3ad6 | 27 | #include <linux/slab.h> |
93ee0a75 LF |
28 | #include <linux/init.h> |
29 | #include <linux/err.h> | |
30 | ||
0c273215 UKK |
31 | #define DRIVER_NAME "mc13783-adc" |
32 | ||
33 | /* platform device id driver data */ | |
34 | #define MC13783_ADC_16CHANS 1 | |
35 | #define MC13783_ADC_BPDIV2 2 | |
93ee0a75 LF |
36 | |
37 | struct mc13783_adc_priv { | |
613c27ab | 38 | struct mc13xxx *mc13xxx; |
93ee0a75 | 39 | struct device *hwmon_dev; |
ae02e741 | 40 | char name[PLATFORM_NAME_SIZE]; |
93ee0a75 LF |
41 | }; |
42 | ||
918b4056 JL |
43 | static ssize_t name_show(struct device *dev, struct device_attribute *devattr, |
44 | char *buf) | |
93ee0a75 | 45 | { |
0c273215 UKK |
46 | struct mc13783_adc_priv *priv = dev_get_drvdata(dev); |
47 | ||
48 | return sprintf(buf, "%s\n", priv->name); | |
93ee0a75 LF |
49 | } |
50 | ||
51 | static int mc13783_adc_read(struct device *dev, | |
52 | struct device_attribute *devattr, unsigned int *val) | |
53 | { | |
0c273215 | 54 | struct mc13783_adc_priv *priv = dev_get_drvdata(dev); |
93ee0a75 LF |
55 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
56 | unsigned int channel = attr->index; | |
57 | unsigned int sample[4]; | |
58 | int ret; | |
59 | ||
613c27ab UKK |
60 | ret = mc13xxx_adc_do_conversion(priv->mc13xxx, |
61 | MC13XXX_ADC_MODE_MULT_CHAN, | |
1039d762 | 62 | channel, 0, 0, sample); |
93ee0a75 LF |
63 | if (ret) |
64 | return ret; | |
65 | ||
ed645ccc AG |
66 | /* ADIN7 subchannels */ |
67 | if (channel >= 16) | |
68 | channel = 7; | |
69 | ||
93ee0a75 LF |
70 | channel &= 0x7; |
71 | ||
72 | *val = (sample[channel % 4] >> (channel > 3 ? 14 : 2)) & 0x3ff; | |
73 | ||
74 | return 0; | |
75 | } | |
76 | ||
77 | static ssize_t mc13783_adc_read_bp(struct device *dev, | |
78 | struct device_attribute *devattr, char *buf) | |
79 | { | |
80 | unsigned val; | |
0c273215 UKK |
81 | struct platform_device *pdev = to_platform_device(dev); |
82 | kernel_ulong_t driver_data = platform_get_device_id(pdev)->driver_data; | |
93ee0a75 LF |
83 | int ret = mc13783_adc_read(dev, devattr, &val); |
84 | ||
85 | if (ret) | |
86 | return ret; | |
87 | ||
0c273215 UKK |
88 | if (driver_data & MC13783_ADC_BPDIV2) |
89 | val = DIV_ROUND_CLOSEST(val * 9, 2); | |
90 | else | |
91 | /* | |
92 | * BP (channel 2) reports with offset 2.4V to the actual value | |
93 | * to fit the input range of the ADC. unit = 2.25mV = 9/4 mV. | |
94 | */ | |
95 | val = DIV_ROUND_CLOSEST(val * 9, 4) + 2400; | |
93ee0a75 LF |
96 | |
97 | return sprintf(buf, "%u\n", val); | |
98 | } | |
99 | ||
100 | static ssize_t mc13783_adc_read_gp(struct device *dev, | |
101 | struct device_attribute *devattr, char *buf) | |
102 | { | |
103 | unsigned val; | |
104 | int ret = mc13783_adc_read(dev, devattr, &val); | |
105 | ||
106 | if (ret) | |
107 | return ret; | |
108 | ||
109 | /* | |
110 | * input range is [0, 2.3V], val has 10 bits, so each bit | |
111 | * is worth 9/4 mV. | |
112 | */ | |
113 | val = DIV_ROUND_CLOSEST(val * 9, 4); | |
114 | ||
115 | return sprintf(buf, "%u\n", val); | |
116 | } | |
117 | ||
ed645ccc AG |
118 | static ssize_t mc13783_adc_read_uid(struct device *dev, |
119 | struct device_attribute *devattr, char *buf) | |
120 | { | |
121 | unsigned int val; | |
122 | struct platform_device *pdev = to_platform_device(dev); | |
123 | kernel_ulong_t driver_data = platform_get_device_id(pdev)->driver_data; | |
124 | int ret = mc13783_adc_read(dev, devattr, &val); | |
125 | ||
126 | if (ret) | |
127 | return ret; | |
128 | ||
129 | if (driver_data & MC13783_ADC_BPDIV2) | |
130 | /* MC13892 have 1/2 divider, input range is [0, 4.800V] */ | |
131 | val = DIV_ROUND_CLOSEST(val * 4800, 1024); | |
132 | else | |
133 | /* MC13783 have 0.9 divider, input range is [0, 2.555V] */ | |
134 | val = DIV_ROUND_CLOSEST(val * 2555, 1024); | |
135 | ||
136 | return sprintf(buf, "%u\n", val); | |
137 | } | |
138 | ||
139 | static ssize_t mc13783_adc_read_temp(struct device *dev, | |
140 | struct device_attribute *devattr, char *buf) | |
141 | { | |
142 | unsigned int val; | |
143 | struct platform_device *pdev = to_platform_device(dev); | |
144 | kernel_ulong_t driver_data = platform_get_device_id(pdev)->driver_data; | |
145 | int ret = mc13783_adc_read(dev, devattr, &val); | |
146 | ||
147 | if (ret) | |
148 | return ret; | |
149 | ||
150 | if (driver_data & MC13783_ADC_BPDIV2) { | |
151 | /* | |
152 | * MC13892: | |
153 | * Die Temperature Read Out Code at 25C 680 | |
154 | * Temperature change per LSB +0.4244C | |
155 | */ | |
156 | ret = DIV_ROUND_CLOSEST(-2635920 + val * 4244, 10); | |
157 | } else { | |
158 | /* | |
159 | * MC13783: | |
160 | * Die Temperature Read Out Code at 25C 282 | |
161 | * Temperature change per LSB -1.14C | |
162 | */ | |
163 | ret = 346480 - 1140 * val; | |
164 | } | |
165 | ||
166 | return sprintf(buf, "%d\n", ret); | |
167 | } | |
168 | ||
918b4056 | 169 | static DEVICE_ATTR_RO(name); |
93ee0a75 LF |
170 | static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, mc13783_adc_read_bp, NULL, 2); |
171 | static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, mc13783_adc_read_gp, NULL, 5); | |
172 | static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, mc13783_adc_read_gp, NULL, 6); | |
173 | static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, mc13783_adc_read_gp, NULL, 7); | |
174 | static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, mc13783_adc_read_gp, NULL, 8); | |
175 | static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, mc13783_adc_read_gp, NULL, 9); | |
176 | static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, mc13783_adc_read_gp, NULL, 10); | |
177 | static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, mc13783_adc_read_gp, NULL, 11); | |
178 | static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, mc13783_adc_read_gp, NULL, 12); | |
179 | static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, mc13783_adc_read_gp, NULL, 13); | |
180 | static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, mc13783_adc_read_gp, NULL, 14); | |
181 | static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, mc13783_adc_read_gp, NULL, 15); | |
ed645ccc AG |
182 | static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, mc13783_adc_read_uid, NULL, 16); |
183 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, | |
184 | mc13783_adc_read_temp, NULL, 17); | |
93ee0a75 | 185 | |
0c273215 | 186 | static struct attribute *mc13783_attr_base[] = { |
93ee0a75 LF |
187 | &dev_attr_name.attr, |
188 | &sensor_dev_attr_in2_input.dev_attr.attr, | |
189 | &sensor_dev_attr_in5_input.dev_attr.attr, | |
190 | &sensor_dev_attr_in6_input.dev_attr.attr, | |
191 | &sensor_dev_attr_in7_input.dev_attr.attr, | |
ed645ccc AG |
192 | &sensor_dev_attr_in16_input.dev_attr.attr, |
193 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
0c273215 UKK |
194 | NULL |
195 | }; | |
196 | ||
197 | static const struct attribute_group mc13783_group_base = { | |
198 | .attrs = mc13783_attr_base, | |
199 | }; | |
200 | ||
201 | /* these are only used if MC13783_ADC_16CHANS is provided in driver data */ | |
202 | static struct attribute *mc13783_attr_16chans[] = { | |
93ee0a75 LF |
203 | &sensor_dev_attr_in8_input.dev_attr.attr, |
204 | &sensor_dev_attr_in9_input.dev_attr.attr, | |
205 | &sensor_dev_attr_in10_input.dev_attr.attr, | |
206 | &sensor_dev_attr_in11_input.dev_attr.attr, | |
207 | NULL | |
208 | }; | |
209 | ||
0c273215 UKK |
210 | static const struct attribute_group mc13783_group_16chans = { |
211 | .attrs = mc13783_attr_16chans, | |
93ee0a75 LF |
212 | }; |
213 | ||
214 | /* last four channels may be occupied by the touchscreen */ | |
215 | static struct attribute *mc13783_attr_ts[] = { | |
216 | &sensor_dev_attr_in12_input.dev_attr.attr, | |
217 | &sensor_dev_attr_in13_input.dev_attr.attr, | |
218 | &sensor_dev_attr_in14_input.dev_attr.attr, | |
219 | &sensor_dev_attr_in15_input.dev_attr.attr, | |
220 | NULL | |
221 | }; | |
222 | ||
223 | static const struct attribute_group mc13783_group_ts = { | |
224 | .attrs = mc13783_attr_ts, | |
225 | }; | |
226 | ||
eaf06ee2 UKK |
227 | static int mc13783_adc_use_touchscreen(struct platform_device *pdev) |
228 | { | |
229 | struct mc13783_adc_priv *priv = platform_get_drvdata(pdev); | |
613c27ab | 230 | unsigned flags = mc13xxx_get_flags(priv->mc13xxx); |
eaf06ee2 | 231 | |
613c27ab | 232 | return flags & MC13XXX_USE_TOUCHSCREEN; |
eaf06ee2 UKK |
233 | } |
234 | ||
93ee0a75 LF |
235 | static int __init mc13783_adc_probe(struct platform_device *pdev) |
236 | { | |
237 | struct mc13783_adc_priv *priv; | |
238 | int ret; | |
0c273215 UKK |
239 | const struct platform_device_id *id = platform_get_device_id(pdev); |
240 | char *dash; | |
93ee0a75 | 241 | |
ed2e073b | 242 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
93ee0a75 LF |
243 | if (!priv) |
244 | return -ENOMEM; | |
245 | ||
613c27ab | 246 | priv->mc13xxx = dev_get_drvdata(pdev->dev.parent); |
0c273215 UKK |
247 | snprintf(priv->name, ARRAY_SIZE(priv->name), "%s", id->name); |
248 | dash = strchr(priv->name, '-'); | |
249 | if (dash) | |
250 | *dash = '\0'; | |
93ee0a75 LF |
251 | |
252 | platform_set_drvdata(pdev, priv); | |
253 | ||
254 | /* Register sysfs hooks */ | |
0c273215 | 255 | ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group_base); |
93ee0a75 | 256 | if (ret) |
ed2e073b | 257 | return ret; |
0c273215 UKK |
258 | |
259 | if (id->driver_data & MC13783_ADC_16CHANS) { | |
260 | ret = sysfs_create_group(&pdev->dev.kobj, | |
261 | &mc13783_group_16chans); | |
262 | if (ret) | |
263 | goto out_err_create_16chans; | |
264 | } | |
93ee0a75 | 265 | |
eaf06ee2 | 266 | if (!mc13783_adc_use_touchscreen(pdev)) { |
93ee0a75 LF |
267 | ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group_ts); |
268 | if (ret) | |
0c273215 | 269 | goto out_err_create_ts; |
eaf06ee2 | 270 | } |
93ee0a75 LF |
271 | |
272 | priv->hwmon_dev = hwmon_device_register(&pdev->dev); | |
273 | if (IS_ERR(priv->hwmon_dev)) { | |
274 | ret = PTR_ERR(priv->hwmon_dev); | |
275 | dev_err(&pdev->dev, | |
276 | "hwmon_device_register failed with %d.\n", ret); | |
277 | goto out_err_register; | |
278 | } | |
279 | ||
93ee0a75 LF |
280 | return 0; |
281 | ||
282 | out_err_register: | |
283 | ||
eaf06ee2 | 284 | if (!mc13783_adc_use_touchscreen(pdev)) |
93ee0a75 | 285 | sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts); |
0c273215 | 286 | out_err_create_ts: |
93ee0a75 | 287 | |
0c273215 UKK |
288 | if (id->driver_data & MC13783_ADC_16CHANS) |
289 | sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_16chans); | |
290 | out_err_create_16chans: | |
291 | ||
292 | sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_base); | |
93ee0a75 LF |
293 | return ret; |
294 | } | |
295 | ||
281dfd0b | 296 | static int mc13783_adc_remove(struct platform_device *pdev) |
93ee0a75 LF |
297 | { |
298 | struct mc13783_adc_priv *priv = platform_get_drvdata(pdev); | |
0c273215 | 299 | kernel_ulong_t driver_data = platform_get_device_id(pdev)->driver_data; |
93ee0a75 LF |
300 | |
301 | hwmon_device_unregister(priv->hwmon_dev); | |
302 | ||
eaf06ee2 | 303 | if (!mc13783_adc_use_touchscreen(pdev)) |
93ee0a75 LF |
304 | sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts); |
305 | ||
0c273215 UKK |
306 | if (driver_data & MC13783_ADC_16CHANS) |
307 | sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_16chans); | |
308 | ||
309 | sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_base); | |
93ee0a75 | 310 | |
93ee0a75 LF |
311 | return 0; |
312 | } | |
313 | ||
0c273215 UKK |
314 | static const struct platform_device_id mc13783_adc_idtable[] = { |
315 | { | |
316 | .name = "mc13783-adc", | |
317 | .driver_data = MC13783_ADC_16CHANS, | |
318 | }, { | |
319 | .name = "mc13892-adc", | |
320 | .driver_data = MC13783_ADC_BPDIV2, | |
321 | }, { | |
322 | /* sentinel */ | |
323 | } | |
324 | }; | |
325 | MODULE_DEVICE_TABLE(platform, mc13783_adc_idtable); | |
326 | ||
93ee0a75 | 327 | static struct platform_driver mc13783_adc_driver = { |
9e5e9b7a | 328 | .remove = mc13783_adc_remove, |
93ee0a75 | 329 | .driver = { |
0c273215 | 330 | .name = DRIVER_NAME, |
93ee0a75 | 331 | }, |
0c273215 | 332 | .id_table = mc13783_adc_idtable, |
93ee0a75 LF |
333 | }; |
334 | ||
f40fb63e | 335 | module_platform_driver_probe(mc13783_adc_driver, mc13783_adc_probe); |
93ee0a75 LF |
336 | |
337 | MODULE_DESCRIPTION("MC13783 ADC driver"); | |
338 | MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>"); | |
339 | MODULE_LICENSE("GPL"); |