xref: /openbmc/linux/drivers/hwmon/mc13783-adc.c (revision 09bae3b6)
1 /*
2  * Driver for the ADC on Freescale Semiconductor MC13783 and MC13892 PMICs.
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 
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>
31 
32 #define DRIVER_NAME	"mc13783-adc"
33 
34 /* platform device id driver data */
35 #define MC13783_ADC_16CHANS	1
36 #define MC13783_ADC_BPDIV2	2
37 
38 struct mc13783_adc_priv {
39 	struct mc13xxx *mc13xxx;
40 	struct device *hwmon_dev;
41 	char name[PLATFORM_NAME_SIZE];
42 };
43 
44 static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
45 			 char *buf)
46 {
47 	struct mc13783_adc_priv *priv = dev_get_drvdata(dev);
48 
49 	return sprintf(buf, "%s\n", priv->name);
50 }
51 
52 static int mc13783_adc_read(struct device *dev,
53 		struct device_attribute *devattr, unsigned int *val)
54 {
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;
60 
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;
66 
67 	/* ADIN7 subchannels */
68 	if (channel >= 16)
69 		channel = 7;
70 
71 	channel &= 0x7;
72 
73 	*val = (sample[channel % 4] >> (channel > 3 ? 14 : 2)) & 0x3ff;
74 
75 	return 0;
76 }
77 
78 static ssize_t mc13783_adc_read_bp(struct device *dev,
79 		struct device_attribute *devattr, char *buf)
80 {
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);
85 
86 	if (ret)
87 		return ret;
88 
89 	if (driver_data & MC13783_ADC_BPDIV2)
90 		val = DIV_ROUND_CLOSEST(val * 9, 2);
91 	else
92 		/*
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.
95 		 */
96 		val = DIV_ROUND_CLOSEST(val * 9, 4) + 2400;
97 
98 	return sprintf(buf, "%u\n", val);
99 }
100 
101 static ssize_t mc13783_adc_read_gp(struct device *dev,
102 		struct device_attribute *devattr, char *buf)
103 {
104 	unsigned val;
105 	int ret = mc13783_adc_read(dev, devattr, &val);
106 
107 	if (ret)
108 		return ret;
109 
110 	/*
111 	 * input range is [0, 2.3V], val has 10 bits, so each bit
112 	 * is worth 9/4 mV.
113 	 */
114 	val = DIV_ROUND_CLOSEST(val * 9, 4);
115 
116 	return sprintf(buf, "%u\n", val);
117 }
118 
119 static ssize_t mc13783_adc_read_uid(struct device *dev,
120 		struct device_attribute *devattr, char *buf)
121 {
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);
126 
127 	if (ret)
128 		return ret;
129 
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);
136 
137 	return sprintf(buf, "%u\n", val);
138 }
139 
140 static ssize_t mc13783_adc_read_temp(struct device *dev,
141 		struct device_attribute *devattr, char *buf)
142 {
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);
147 
148 	if (ret)
149 		return ret;
150 
151 	if (driver_data & MC13783_ADC_BPDIV2) {
152 		/*
153 		 * MC13892:
154 		 * Die Temperature Read Out Code at 25C 680
155 		 * Temperature change per LSB +0.4244C
156 		 */
157 		ret = DIV_ROUND_CLOSEST(-2635920 + val * 4244, 10);
158 	} else {
159 		/*
160 		 * MC13783:
161 		 * Die Temperature Read Out Code at 25C 282
162 		 * Temperature change per LSB -1.14C
163 		 */
164 		ret = 346480 - 1140 * val;
165 	}
166 
167 	return sprintf(buf, "%d\n", ret);
168 }
169 
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);
186 
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
196 };
197 
198 static const struct attribute_group mc13783_group_base = {
199 	.attrs = mc13783_attr_base,
200 };
201 
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
209 };
210 
211 static const struct attribute_group mc13783_group_16chans = {
212 	.attrs = mc13783_attr_16chans,
213 };
214 
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
222 };
223 
224 static const struct attribute_group mc13783_group_ts = {
225 	.attrs = mc13783_attr_ts,
226 };
227 
228 static int mc13783_adc_use_touchscreen(struct platform_device *pdev)
229 {
230 	struct mc13783_adc_priv *priv = platform_get_drvdata(pdev);
231 	unsigned flags = mc13xxx_get_flags(priv->mc13xxx);
232 
233 	return flags & MC13XXX_USE_TOUCHSCREEN;
234 }
235 
236 static int __init mc13783_adc_probe(struct platform_device *pdev)
237 {
238 	struct mc13783_adc_priv *priv;
239 	int ret;
240 	const struct platform_device_id *id = platform_get_device_id(pdev);
241 	char *dash;
242 
243 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
244 	if (!priv)
245 		return -ENOMEM;
246 
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';
252 
253 	platform_set_drvdata(pdev, priv);
254 
255 	/* Register sysfs hooks */
256 	ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group_base);
257 	if (ret)
258 		return ret;
259 
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;
265 	}
266 
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;
271 	}
272 
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;
279 	}
280 
281 	return 0;
282 
283 out_err_register:
284 
285 	if (!mc13783_adc_use_touchscreen(pdev))
286 		sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts);
287 out_err_create_ts:
288 
289 	if (id->driver_data & MC13783_ADC_16CHANS)
290 		sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_16chans);
291 out_err_create_16chans:
292 
293 	sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_base);
294 	return ret;
295 }
296 
297 static int mc13783_adc_remove(struct platform_device *pdev)
298 {
299 	struct mc13783_adc_priv *priv = platform_get_drvdata(pdev);
300 	kernel_ulong_t driver_data = platform_get_device_id(pdev)->driver_data;
301 
302 	hwmon_device_unregister(priv->hwmon_dev);
303 
304 	if (!mc13783_adc_use_touchscreen(pdev))
305 		sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts);
306 
307 	if (driver_data & MC13783_ADC_16CHANS)
308 		sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_16chans);
309 
310 	sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_base);
311 
312 	return 0;
313 }
314 
315 static const struct platform_device_id mc13783_adc_idtable[] = {
316 	{
317 		.name = "mc13783-adc",
318 		.driver_data = MC13783_ADC_16CHANS,
319 	}, {
320 		.name = "mc13892-adc",
321 		.driver_data = MC13783_ADC_BPDIV2,
322 	}, {
323 		/* sentinel */
324 	}
325 };
326 MODULE_DEVICE_TABLE(platform, mc13783_adc_idtable);
327 
328 static struct platform_driver mc13783_adc_driver = {
329 	.remove		= mc13783_adc_remove,
330 	.driver		= {
331 		.name	= DRIVER_NAME,
332 	},
333 	.id_table	= mc13783_adc_idtable,
334 };
335 
336 module_platform_driver_probe(mc13783_adc_driver, mc13783_adc_probe);
337 
338 MODULE_DESCRIPTION("MC13783 ADC driver");
339 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
340 MODULE_LICENSE("GPL");
341