blob: 67860ad2e3d987e3d322a43b652ece858eed5ee0 [file] [log] [blame]
Luotao Fu93ee0a72009-12-09 20:35:58 +01001/*
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +01002 * Driver for the ADC on Freescale Semiconductor MC13783 and MC13892 PMICs.
Luotao Fu93ee0a72009-12-09 20:35:58 +01003 *
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
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +010021#include <linux/mfd/mc13xxx.h>
Luotao Fu93ee0a72009-12-09 20:35:58 +010022#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Luotao Fu93ee0a72009-12-09 20:35:58 +010028#include <linux/init.h>
29#include <linux/err.h>
30
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +010031#define DRIVER_NAME "mc13783-adc"
32
33/* platform device id driver data */
34#define MC13783_ADC_16CHANS 1
35#define MC13783_ADC_BPDIV2 2
Luotao Fu93ee0a72009-12-09 20:35:58 +010036
37struct mc13783_adc_priv {
Uwe Kleine-König613c27a2011-08-24 15:28:24 +020038 struct mc13xxx *mc13xxx;
Luotao Fu93ee0a72009-12-09 20:35:58 +010039 struct device *hwmon_dev;
Guenter Roeckae02e742013-09-13 10:25:53 -070040 char name[PLATFORM_NAME_SIZE];
Luotao Fu93ee0a72009-12-09 20:35:58 +010041};
42
Julia Lawall918b40562016-12-22 13:04:59 +010043static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
44 char *buf)
Luotao Fu93ee0a72009-12-09 20:35:58 +010045{
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +010046 struct mc13783_adc_priv *priv = dev_get_drvdata(dev);
47
48 return sprintf(buf, "%s\n", priv->name);
Luotao Fu93ee0a72009-12-09 20:35:58 +010049}
50
51static int mc13783_adc_read(struct device *dev,
52 struct device_attribute *devattr, unsigned int *val)
53{
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +010054 struct mc13783_adc_priv *priv = dev_get_drvdata(dev);
Luotao Fu93ee0a72009-12-09 20:35:58 +010055 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
Uwe Kleine-König613c27a2011-08-24 15:28:24 +020060 ret = mc13xxx_adc_do_conversion(priv->mc13xxx,
61 MC13XXX_ADC_MODE_MULT_CHAN,
Michael Thalmeier1039d762012-02-20 12:18:13 +010062 channel, 0, 0, sample);
Luotao Fu93ee0a72009-12-09 20:35:58 +010063 if (ret)
64 return ret;
65
Andrey Gusakoved645cc2018-03-27 17:19:49 +030066 /* ADIN7 subchannels */
67 if (channel >= 16)
68 channel = 7;
69
Luotao Fu93ee0a72009-12-09 20:35:58 +010070 channel &= 0x7;
71
72 *val = (sample[channel % 4] >> (channel > 3 ? 14 : 2)) & 0x3ff;
73
74 return 0;
75}
76
77static ssize_t mc13783_adc_read_bp(struct device *dev,
78 struct device_attribute *devattr, char *buf)
79{
80 unsigned val;
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +010081 struct platform_device *pdev = to_platform_device(dev);
82 kernel_ulong_t driver_data = platform_get_device_id(pdev)->driver_data;
Luotao Fu93ee0a72009-12-09 20:35:58 +010083 int ret = mc13783_adc_read(dev, devattr, &val);
84
85 if (ret)
86 return ret;
87
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +010088 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;
Luotao Fu93ee0a72009-12-09 20:35:58 +010096
97 return sprintf(buf, "%u\n", val);
98}
99
100static 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
Andrey Gusakoved645cc2018-03-27 17:19:49 +0300118static 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
139static 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
Julia Lawall918b40562016-12-22 13:04:59 +0100169static DEVICE_ATTR_RO(name);
Luotao Fu93ee0a72009-12-09 20:35:58 +0100170static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, mc13783_adc_read_bp, NULL, 2);
171static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, mc13783_adc_read_gp, NULL, 5);
172static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, mc13783_adc_read_gp, NULL, 6);
173static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, mc13783_adc_read_gp, NULL, 7);
174static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, mc13783_adc_read_gp, NULL, 8);
175static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, mc13783_adc_read_gp, NULL, 9);
176static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, mc13783_adc_read_gp, NULL, 10);
177static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, mc13783_adc_read_gp, NULL, 11);
178static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, mc13783_adc_read_gp, NULL, 12);
179static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, mc13783_adc_read_gp, NULL, 13);
180static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, mc13783_adc_read_gp, NULL, 14);
181static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, mc13783_adc_read_gp, NULL, 15);
Andrey Gusakoved645cc2018-03-27 17:19:49 +0300182static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, mc13783_adc_read_uid, NULL, 16);
183static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
184 mc13783_adc_read_temp, NULL, 17);
Luotao Fu93ee0a72009-12-09 20:35:58 +0100185
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100186static struct attribute *mc13783_attr_base[] = {
Luotao Fu93ee0a72009-12-09 20:35:58 +0100187 &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,
Andrey Gusakoved645cc2018-03-27 17:19:49 +0300192 &sensor_dev_attr_in16_input.dev_attr.attr,
193 &sensor_dev_attr_temp1_input.dev_attr.attr,
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100194 NULL
195};
196
197static 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 */
202static struct attribute *mc13783_attr_16chans[] = {
Luotao Fu93ee0a72009-12-09 20:35:58 +0100203 &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
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100210static const struct attribute_group mc13783_group_16chans = {
211 .attrs = mc13783_attr_16chans,
Luotao Fu93ee0a72009-12-09 20:35:58 +0100212};
213
214/* last four channels may be occupied by the touchscreen */
215static 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
223static const struct attribute_group mc13783_group_ts = {
224 .attrs = mc13783_attr_ts,
225};
226
Uwe Kleine-Königeaf06ee2010-08-02 11:14:18 +0200227static int mc13783_adc_use_touchscreen(struct platform_device *pdev)
228{
229 struct mc13783_adc_priv *priv = platform_get_drvdata(pdev);
Uwe Kleine-König613c27a2011-08-24 15:28:24 +0200230 unsigned flags = mc13xxx_get_flags(priv->mc13xxx);
Uwe Kleine-Königeaf06ee2010-08-02 11:14:18 +0200231
Uwe Kleine-König613c27a2011-08-24 15:28:24 +0200232 return flags & MC13XXX_USE_TOUCHSCREEN;
Uwe Kleine-Königeaf06ee2010-08-02 11:14:18 +0200233}
234
Luotao Fu93ee0a72009-12-09 20:35:58 +0100235static int __init mc13783_adc_probe(struct platform_device *pdev)
236{
237 struct mc13783_adc_priv *priv;
238 int ret;
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100239 const struct platform_device_id *id = platform_get_device_id(pdev);
240 char *dash;
Luotao Fu93ee0a72009-12-09 20:35:58 +0100241
Guenter Roecked2e0732012-06-02 09:49:11 -0700242 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Luotao Fu93ee0a72009-12-09 20:35:58 +0100243 if (!priv)
244 return -ENOMEM;
245
Uwe Kleine-König613c27a2011-08-24 15:28:24 +0200246 priv->mc13xxx = dev_get_drvdata(pdev->dev.parent);
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100247 snprintf(priv->name, ARRAY_SIZE(priv->name), "%s", id->name);
248 dash = strchr(priv->name, '-');
249 if (dash)
250 *dash = '\0';
Luotao Fu93ee0a72009-12-09 20:35:58 +0100251
252 platform_set_drvdata(pdev, priv);
253
254 /* Register sysfs hooks */
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100255 ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group_base);
Luotao Fu93ee0a72009-12-09 20:35:58 +0100256 if (ret)
Guenter Roecked2e0732012-06-02 09:49:11 -0700257 return ret;
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100258
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 }
Luotao Fu93ee0a72009-12-09 20:35:58 +0100265
Uwe Kleine-Königeaf06ee2010-08-02 11:14:18 +0200266 if (!mc13783_adc_use_touchscreen(pdev)) {
Luotao Fu93ee0a72009-12-09 20:35:58 +0100267 ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group_ts);
268 if (ret)
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100269 goto out_err_create_ts;
Uwe Kleine-Königeaf06ee2010-08-02 11:14:18 +0200270 }
Luotao Fu93ee0a72009-12-09 20:35:58 +0100271
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
Luotao Fu93ee0a72009-12-09 20:35:58 +0100280 return 0;
281
282out_err_register:
283
Uwe Kleine-Königeaf06ee2010-08-02 11:14:18 +0200284 if (!mc13783_adc_use_touchscreen(pdev))
Luotao Fu93ee0a72009-12-09 20:35:58 +0100285 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts);
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100286out_err_create_ts:
Luotao Fu93ee0a72009-12-09 20:35:58 +0100287
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100288 if (id->driver_data & MC13783_ADC_16CHANS)
289 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_16chans);
290out_err_create_16chans:
291
292 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_base);
Luotao Fu93ee0a72009-12-09 20:35:58 +0100293 return ret;
294}
295
Bill Pemberton281dfd02012-11-19 13:25:51 -0500296static int mc13783_adc_remove(struct platform_device *pdev)
Luotao Fu93ee0a72009-12-09 20:35:58 +0100297{
298 struct mc13783_adc_priv *priv = platform_get_drvdata(pdev);
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100299 kernel_ulong_t driver_data = platform_get_device_id(pdev)->driver_data;
Luotao Fu93ee0a72009-12-09 20:35:58 +0100300
301 hwmon_device_unregister(priv->hwmon_dev);
302
Uwe Kleine-Königeaf06ee2010-08-02 11:14:18 +0200303 if (!mc13783_adc_use_touchscreen(pdev))
Luotao Fu93ee0a72009-12-09 20:35:58 +0100304 sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts);
305
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100306 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);
Luotao Fu93ee0a72009-12-09 20:35:58 +0100310
Luotao Fu93ee0a72009-12-09 20:35:58 +0100311 return 0;
312}
313
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100314static 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};
325MODULE_DEVICE_TABLE(platform, mc13783_adc_idtable);
326
Luotao Fu93ee0a72009-12-09 20:35:58 +0100327static struct platform_driver mc13783_adc_driver = {
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500328 .remove = mc13783_adc_remove,
Luotao Fu93ee0a72009-12-09 20:35:58 +0100329 .driver = {
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100330 .name = DRIVER_NAME,
Luotao Fu93ee0a72009-12-09 20:35:58 +0100331 },
Uwe Kleine-Koenig0c273212012-03-23 10:02:19 +0100332 .id_table = mc13783_adc_idtable,
Luotao Fu93ee0a72009-12-09 20:35:58 +0100333};
334
Jingoo Hanf40fb632013-03-05 12:12:55 +0900335module_platform_driver_probe(mc13783_adc_driver, mc13783_adc_probe);
Luotao Fu93ee0a72009-12-09 20:35:58 +0100336
337MODULE_DESCRIPTION("MC13783 ADC driver");
338MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
339MODULE_LICENSE("GPL");