Linux 4.19.133
[linux/fpc-iii.git] / drivers / hwmon / mc13783-adc.c
blob78fe8759d2a9448f12bb9ae02ee0101643a1e74e
1 /*
2 * Driver for the ADC on Freescale Semiconductor MC13783 and MC13892 PMICs.
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/mc13xxx.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/mod_devicetable.h>
27 #include <linux/hwmon.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/err.h>
32 #define DRIVER_NAME "mc13783-adc"
34 /* platform device id driver data */
35 #define MC13783_ADC_16CHANS 1
36 #define MC13783_ADC_BPDIV2 2
38 struct mc13783_adc_priv {
39 struct mc13xxx *mc13xxx;
40 struct device *hwmon_dev;
41 char name[PLATFORM_NAME_SIZE];
44 static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
45 char *buf)
47 struct mc13783_adc_priv *priv = dev_get_drvdata(dev);
49 return sprintf(buf, "%s\n", priv->name);
52 static int mc13783_adc_read(struct device *dev,
53 struct device_attribute *devattr, unsigned int *val)
55 struct mc13783_adc_priv *priv = dev_get_drvdata(dev);
56 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
57 unsigned int channel = attr->index;
58 unsigned int sample[4];
59 int ret;
61 ret = mc13xxx_adc_do_conversion(priv->mc13xxx,
62 MC13XXX_ADC_MODE_MULT_CHAN,
63 channel, 0, 0, sample);
64 if (ret)
65 return ret;
67 /* ADIN7 subchannels */
68 if (channel >= 16)
69 channel = 7;
71 channel &= 0x7;
73 *val = (sample[channel % 4] >> (channel > 3 ? 14 : 2)) & 0x3ff;
75 return 0;
78 static ssize_t mc13783_adc_read_bp(struct device *dev,
79 struct device_attribute *devattr, char *buf)
81 unsigned val;
82 struct platform_device *pdev = to_platform_device(dev);
83 kernel_ulong_t driver_data = platform_get_device_id(pdev)->driver_data;
84 int ret = mc13783_adc_read(dev, devattr, &val);
86 if (ret)
87 return ret;
89 if (driver_data & MC13783_ADC_BPDIV2)
90 val = DIV_ROUND_CLOSEST(val * 9, 2);
91 else
93 * BP (channel 2) reports with offset 2.4V to the actual value
94 * to fit the input range of the ADC. unit = 2.25mV = 9/4 mV.
96 val = DIV_ROUND_CLOSEST(val * 9, 4) + 2400;
98 return sprintf(buf, "%u\n", val);
101 static ssize_t mc13783_adc_read_gp(struct device *dev,
102 struct device_attribute *devattr, char *buf)
104 unsigned val;
105 int ret = mc13783_adc_read(dev, devattr, &val);
107 if (ret)
108 return ret;
111 * input range is [0, 2.3V], val has 10 bits, so each bit
112 * is worth 9/4 mV.
114 val = DIV_ROUND_CLOSEST(val * 9, 4);
116 return sprintf(buf, "%u\n", val);
119 static ssize_t mc13783_adc_read_uid(struct device *dev,
120 struct device_attribute *devattr, char *buf)
122 unsigned int val;
123 struct platform_device *pdev = to_platform_device(dev);
124 kernel_ulong_t driver_data = platform_get_device_id(pdev)->driver_data;
125 int ret = mc13783_adc_read(dev, devattr, &val);
127 if (ret)
128 return ret;
130 if (driver_data & MC13783_ADC_BPDIV2)
131 /* MC13892 have 1/2 divider, input range is [0, 4.800V] */
132 val = DIV_ROUND_CLOSEST(val * 4800, 1024);
133 else
134 /* MC13783 have 0.9 divider, input range is [0, 2.555V] */
135 val = DIV_ROUND_CLOSEST(val * 2555, 1024);
137 return sprintf(buf, "%u\n", val);
140 static ssize_t mc13783_adc_read_temp(struct device *dev,
141 struct device_attribute *devattr, char *buf)
143 unsigned int val;
144 struct platform_device *pdev = to_platform_device(dev);
145 kernel_ulong_t driver_data = platform_get_device_id(pdev)->driver_data;
146 int ret = mc13783_adc_read(dev, devattr, &val);
148 if (ret)
149 return ret;
151 if (driver_data & MC13783_ADC_BPDIV2) {
153 * MC13892:
154 * Die Temperature Read Out Code at 25C 680
155 * Temperature change per LSB +0.4244C
157 ret = DIV_ROUND_CLOSEST(-2635920 + val * 4244, 10);
158 } else {
160 * MC13783:
161 * Die Temperature Read Out Code at 25C 282
162 * Temperature change per LSB -1.14C
164 ret = 346480 - 1140 * val;
167 return sprintf(buf, "%d\n", ret);
170 static DEVICE_ATTR_RO(name);
171 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, mc13783_adc_read_bp, NULL, 2);
172 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, mc13783_adc_read_gp, NULL, 5);
173 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, mc13783_adc_read_gp, NULL, 6);
174 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, mc13783_adc_read_gp, NULL, 7);
175 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, mc13783_adc_read_gp, NULL, 8);
176 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, mc13783_adc_read_gp, NULL, 9);
177 static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, mc13783_adc_read_gp, NULL, 10);
178 static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, mc13783_adc_read_gp, NULL, 11);
179 static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, mc13783_adc_read_gp, NULL, 12);
180 static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, mc13783_adc_read_gp, NULL, 13);
181 static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, mc13783_adc_read_gp, NULL, 14);
182 static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, mc13783_adc_read_gp, NULL, 15);
183 static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, mc13783_adc_read_uid, NULL, 16);
184 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
185 mc13783_adc_read_temp, NULL, 17);
187 static struct attribute *mc13783_attr_base[] = {
188 &dev_attr_name.attr,
189 &sensor_dev_attr_in2_input.dev_attr.attr,
190 &sensor_dev_attr_in5_input.dev_attr.attr,
191 &sensor_dev_attr_in6_input.dev_attr.attr,
192 &sensor_dev_attr_in7_input.dev_attr.attr,
193 &sensor_dev_attr_in16_input.dev_attr.attr,
194 &sensor_dev_attr_temp1_input.dev_attr.attr,
195 NULL
198 static const struct attribute_group mc13783_group_base = {
199 .attrs = mc13783_attr_base,
202 /* these are only used if MC13783_ADC_16CHANS is provided in driver data */
203 static struct attribute *mc13783_attr_16chans[] = {
204 &sensor_dev_attr_in8_input.dev_attr.attr,
205 &sensor_dev_attr_in9_input.dev_attr.attr,
206 &sensor_dev_attr_in10_input.dev_attr.attr,
207 &sensor_dev_attr_in11_input.dev_attr.attr,
208 NULL
211 static const struct attribute_group mc13783_group_16chans = {
212 .attrs = mc13783_attr_16chans,
215 /* last four channels may be occupied by the touchscreen */
216 static struct attribute *mc13783_attr_ts[] = {
217 &sensor_dev_attr_in12_input.dev_attr.attr,
218 &sensor_dev_attr_in13_input.dev_attr.attr,
219 &sensor_dev_attr_in14_input.dev_attr.attr,
220 &sensor_dev_attr_in15_input.dev_attr.attr,
221 NULL
224 static const struct attribute_group mc13783_group_ts = {
225 .attrs = mc13783_attr_ts,
228 static int mc13783_adc_use_touchscreen(struct platform_device *pdev)
230 struct mc13783_adc_priv *priv = platform_get_drvdata(pdev);
231 unsigned flags = mc13xxx_get_flags(priv->mc13xxx);
233 return flags & MC13XXX_USE_TOUCHSCREEN;
236 static int __init mc13783_adc_probe(struct platform_device *pdev)
238 struct mc13783_adc_priv *priv;
239 int ret;
240 const struct platform_device_id *id = platform_get_device_id(pdev);
241 char *dash;
243 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
244 if (!priv)
245 return -ENOMEM;
247 priv->mc13xxx = dev_get_drvdata(pdev->dev.parent);
248 snprintf(priv->name, ARRAY_SIZE(priv->name), "%s", id->name);
249 dash = strchr(priv->name, '-');
250 if (dash)
251 *dash = '\0';
253 platform_set_drvdata(pdev, priv);
255 /* Register sysfs hooks */
256 ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group_base);
257 if (ret)
258 return ret;
260 if (id->driver_data & MC13783_ADC_16CHANS) {
261 ret = sysfs_create_group(&pdev->dev.kobj,
262 &mc13783_group_16chans);
263 if (ret)
264 goto out_err_create_16chans;
267 if (!mc13783_adc_use_touchscreen(pdev)) {
268 ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group_ts);
269 if (ret)
270 goto out_err_create_ts;
273 priv->hwmon_dev = hwmon_device_register(&pdev->dev);
274 if (IS_ERR(priv->hwmon_dev)) {
275 ret = PTR_ERR(priv->hwmon_dev);
276 dev_err(&pdev->dev,
277 "hwmon_device_register failed with %d.\n", ret);
278 goto out_err_register;
281 return 0;
283 out_err_register:
285 if (!mc13783_adc_use_touchscreen(pdev))
286 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts);
287 out_err_create_ts:
289 if (id->driver_data & MC13783_ADC_16CHANS)
290 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_16chans);
291 out_err_create_16chans:
293 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_base);
294 return ret;
297 static int mc13783_adc_remove(struct platform_device *pdev)
299 struct mc13783_adc_priv *priv = platform_get_drvdata(pdev);
300 kernel_ulong_t driver_data = platform_get_device_id(pdev)->driver_data;
302 hwmon_device_unregister(priv->hwmon_dev);
304 if (!mc13783_adc_use_touchscreen(pdev))
305 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts);
307 if (driver_data & MC13783_ADC_16CHANS)
308 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_16chans);
310 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_base);
312 return 0;
315 static const struct platform_device_id mc13783_adc_idtable[] = {
317 .name = "mc13783-adc",
318 .driver_data = MC13783_ADC_16CHANS,
319 }, {
320 .name = "mc13892-adc",
321 .driver_data = MC13783_ADC_BPDIV2,
322 }, {
323 /* sentinel */
326 MODULE_DEVICE_TABLE(platform, mc13783_adc_idtable);
328 static struct platform_driver mc13783_adc_driver = {
329 .remove = mc13783_adc_remove,
330 .driver = {
331 .name = DRIVER_NAME,
333 .id_table = mc13783_adc_idtable,
336 module_platform_driver_probe(mc13783_adc_driver, mc13783_adc_probe);
338 MODULE_DESCRIPTION("MC13783 ADC driver");
339 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
340 MODULE_LICENSE("GPL");