blob: ff2ae0252a48b048d9e4844efa5182c53b1e6188 [file] [log] [blame]
Ben Dooksbff78652009-07-18 10:12:28 +01001/* linux/drivers/hwmon/s3c-hwmon.c
2 *
3 * Copyright (C) 2005, 2008, 2009 Simtec Electronics
4 * http://armlinux.simtec.co.uk/
5 * Ben Dooks <ben@simtec.co.uk>
6 *
7 * S3C24XX/S3C64XX ADC hwmon support
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#include <linux/module.h>
24#include <linux/slab.h>
Ben Dooksbff78652009-07-18 10:12:28 +010025#include <linux/io.h>
26#include <linux/init.h>
27#include <linux/err.h>
28#include <linux/clk.h>
29#include <linux/interrupt.h>
30#include <linux/platform_device.h>
31
32#include <linux/hwmon.h>
33#include <linux/hwmon-sysfs.h>
34
35#include <plat/adc.h>
Arnd Bergmann436d42c2012-08-24 15:22:12 +020036#include <linux/platform_data/hwmon-s3c.h>
Ben Dooksbff78652009-07-18 10:12:28 +010037
38struct s3c_hwmon_attr {
39 struct sensor_device_attribute in;
40 struct sensor_device_attribute label;
41 char in_name[12];
42 char label_name[12];
43};
44
45/**
46 * struct s3c_hwmon - ADC hwmon client information
47 * @lock: Access lock to serialise the conversions.
48 * @client: The client we registered with the S3C ADC core.
49 * @hwmon_dev: The hwmon device we created.
50 * @attr: The holders for the channel attributes.
51*/
52struct s3c_hwmon {
Thomas Gleixner9c627e32010-10-28 20:31:44 +020053 struct mutex lock;
Ben Dooksbff78652009-07-18 10:12:28 +010054 struct s3c_adc_client *client;
55 struct device *hwmon_dev;
56
57 struct s3c_hwmon_attr attrs[8];
58};
59
60/**
61 * s3c_hwmon_read_ch - read a value from a given adc channel.
62 * @dev: The device.
63 * @hwmon: Our state.
64 * @channel: The channel we're reading from.
65 *
66 * Read a value from the @channel with the proper locking and sleep until
67 * either the read completes or we timeout awaiting the ADC core to get
68 * back to us.
69 */
70static int s3c_hwmon_read_ch(struct device *dev,
71 struct s3c_hwmon *hwmon, int channel)
72{
73 int ret;
74
Thomas Gleixner9c627e32010-10-28 20:31:44 +020075 ret = mutex_lock_interruptible(&hwmon->lock);
Ben Dooksbff78652009-07-18 10:12:28 +010076 if (ret < 0)
77 return ret;
78
79 dev_dbg(dev, "reading channel %d\n", channel);
80
81 ret = s3c_adc_read(hwmon->client, channel);
Thomas Gleixner9c627e32010-10-28 20:31:44 +020082 mutex_unlock(&hwmon->lock);
Ben Dooksbff78652009-07-18 10:12:28 +010083
84 return ret;
85}
86
87#ifdef CONFIG_SENSORS_S3C_RAW
88/**
89 * s3c_hwmon_show_raw - show a conversion from the raw channel number.
90 * @dev: The device that the attribute belongs to.
91 * @attr: The attribute being read.
92 * @buf: The result buffer.
93 *
94 * This show deals with the raw attribute, registered for each possible
95 * ADC channel. This does a conversion and returns the raw (un-scaled)
96 * value returned from the hardware.
97 */
98static ssize_t s3c_hwmon_show_raw(struct device *dev,
99 struct device_attribute *attr, char *buf)
100{
101 struct s3c_hwmon *adc = platform_get_drvdata(to_platform_device(dev));
102 struct sensor_device_attribute *sa = to_sensor_dev_attr(attr);
103 int ret;
104
105 ret = s3c_hwmon_read_ch(dev, adc, sa->index);
106
107 return (ret < 0) ? ret : snprintf(buf, PAGE_SIZE, "%d\n", ret);
108}
109
110#define DEF_ADC_ATTR(x) \
111 static SENSOR_DEVICE_ATTR(adc##x##_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, x)
112
113DEF_ADC_ATTR(0);
114DEF_ADC_ATTR(1);
115DEF_ADC_ATTR(2);
116DEF_ADC_ATTR(3);
117DEF_ADC_ATTR(4);
118DEF_ADC_ATTR(5);
119DEF_ADC_ATTR(6);
120DEF_ADC_ATTR(7);
121
122static struct attribute *s3c_hwmon_attrs[9] = {
123 &sensor_dev_attr_adc0_raw.dev_attr.attr,
124 &sensor_dev_attr_adc1_raw.dev_attr.attr,
125 &sensor_dev_attr_adc2_raw.dev_attr.attr,
126 &sensor_dev_attr_adc3_raw.dev_attr.attr,
127 &sensor_dev_attr_adc4_raw.dev_attr.attr,
128 &sensor_dev_attr_adc5_raw.dev_attr.attr,
129 &sensor_dev_attr_adc6_raw.dev_attr.attr,
130 &sensor_dev_attr_adc7_raw.dev_attr.attr,
131 NULL,
132};
133
134static struct attribute_group s3c_hwmon_attrgroup = {
135 .attrs = s3c_hwmon_attrs,
136};
137
138static inline int s3c_hwmon_add_raw(struct device *dev)
139{
140 return sysfs_create_group(&dev->kobj, &s3c_hwmon_attrgroup);
141}
142
143static inline void s3c_hwmon_remove_raw(struct device *dev)
144{
145 sysfs_remove_group(&dev->kobj, &s3c_hwmon_attrgroup);
146}
147
148#else
149
150static inline int s3c_hwmon_add_raw(struct device *dev) { return 0; }
151static inline void s3c_hwmon_remove_raw(struct device *dev) { }
152
153#endif /* CONFIG_SENSORS_S3C_RAW */
154
155/**
156 * s3c_hwmon_ch_show - show value of a given channel
157 * @dev: The device that the attribute belongs to.
158 * @attr: The attribute being read.
159 * @buf: The result buffer.
160 *
161 * Read a value from the ADC and scale it before returning it to the
162 * caller. The scale factor is gained from the channel configuration
163 * passed via the platform data when the device was registered.
164 */
165static ssize_t s3c_hwmon_ch_show(struct device *dev,
166 struct device_attribute *attr,
167 char *buf)
168{
169 struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
170 struct s3c_hwmon *hwmon = platform_get_drvdata(to_platform_device(dev));
171 struct s3c_hwmon_pdata *pdata = dev->platform_data;
172 struct s3c_hwmon_chcfg *cfg;
173 int ret;
174
175 cfg = pdata->in[sen_attr->index];
176
177 ret = s3c_hwmon_read_ch(dev, hwmon, sen_attr->index);
178 if (ret < 0)
179 return ret;
180
181 ret *= cfg->mult;
182 ret = DIV_ROUND_CLOSEST(ret, cfg->div);
183
184 return snprintf(buf, PAGE_SIZE, "%d\n", ret);
185}
186
187/**
188 * s3c_hwmon_label_show - show label name of the given channel.
189 * @dev: The device that the attribute belongs to.
190 * @attr: The attribute being read.
191 * @buf: The result buffer.
192 *
193 * Return the label name of a given channel
194 */
195static ssize_t s3c_hwmon_label_show(struct device *dev,
196 struct device_attribute *attr,
197 char *buf)
198{
199 struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
200 struct s3c_hwmon_pdata *pdata = dev->platform_data;
201 struct s3c_hwmon_chcfg *cfg;
202
203 cfg = pdata->in[sen_attr->index];
204
205 return snprintf(buf, PAGE_SIZE, "%s\n", cfg->name);
206}
207
208/**
209 * s3c_hwmon_create_attr - create hwmon attribute for given channel.
210 * @dev: The device to create the attribute on.
211 * @cfg: The channel configuration passed from the platform data.
212 * @channel: The ADC channel number to process.
213 *
214 * Create the scaled attribute for use with hwmon from the specified
215 * platform data in @pdata. The sysfs entry is handled by the routine
216 * s3c_hwmon_ch_show().
217 *
218 * The attribute name is taken from the configuration data if present
219 * otherwise the name is taken by concatenating in_ with the channel
220 * number.
221 */
222static int s3c_hwmon_create_attr(struct device *dev,
223 struct s3c_hwmon_chcfg *cfg,
224 struct s3c_hwmon_attr *attrs,
225 int channel)
226{
227 struct sensor_device_attribute *attr;
228 int ret;
229
230 snprintf(attrs->in_name, sizeof(attrs->in_name), "in%d_input", channel);
231
232 attr = &attrs->in;
233 attr->index = channel;
Guenter Roeckb1e698d2011-05-24 12:34:55 -0700234 sysfs_attr_init(&attr->dev_attr.attr);
Ben Dooksbff78652009-07-18 10:12:28 +0100235 attr->dev_attr.attr.name = attrs->in_name;
236 attr->dev_attr.attr.mode = S_IRUGO;
Ben Dooksbff78652009-07-18 10:12:28 +0100237 attr->dev_attr.show = s3c_hwmon_ch_show;
238
239 ret = device_create_file(dev, &attr->dev_attr);
240 if (ret < 0) {
241 dev_err(dev, "failed to create input attribute\n");
242 return ret;
243 }
244
245 /* if this has a name, add a label */
246 if (cfg->name) {
247 snprintf(attrs->label_name, sizeof(attrs->label_name),
248 "in%d_label", channel);
249
250 attr = &attrs->label;
251 attr->index = channel;
Guenter Roeckb1e698d2011-05-24 12:34:55 -0700252 sysfs_attr_init(&attr->dev_attr.attr);
Ben Dooksbff78652009-07-18 10:12:28 +0100253 attr->dev_attr.attr.name = attrs->label_name;
254 attr->dev_attr.attr.mode = S_IRUGO;
Ben Dooksbff78652009-07-18 10:12:28 +0100255 attr->dev_attr.show = s3c_hwmon_label_show;
256
257 ret = device_create_file(dev, &attr->dev_attr);
258 if (ret < 0) {
259 device_remove_file(dev, &attrs->in.dev_attr);
260 dev_err(dev, "failed to create label attribute\n");
261 }
262 }
263
264 return ret;
265}
266
267static void s3c_hwmon_remove_attr(struct device *dev,
268 struct s3c_hwmon_attr *attrs)
269{
270 device_remove_file(dev, &attrs->in.dev_attr);
271 device_remove_file(dev, &attrs->label.dev_attr);
272}
273
274/**
275 * s3c_hwmon_probe - device probe entry.
276 * @dev: The device being probed.
277*/
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500278static int s3c_hwmon_probe(struct platform_device *dev)
Ben Dooksbff78652009-07-18 10:12:28 +0100279{
280 struct s3c_hwmon_pdata *pdata = dev->dev.platform_data;
281 struct s3c_hwmon *hwmon;
282 int ret = 0;
283 int i;
284
285 if (!pdata) {
286 dev_err(&dev->dev, "no platform data supplied\n");
287 return -EINVAL;
288 }
289
Sachin Kamat41bf8702012-07-13 14:36:28 +0530290 hwmon = devm_kzalloc(&dev->dev, sizeof(struct s3c_hwmon), GFP_KERNEL);
Ben Dooksbff78652009-07-18 10:12:28 +0100291 if (hwmon == NULL) {
292 dev_err(&dev->dev, "no memory\n");
293 return -ENOMEM;
294 }
295
296 platform_set_drvdata(dev, hwmon);
297
Thomas Gleixner9c627e32010-10-28 20:31:44 +0200298 mutex_init(&hwmon->lock);
Ben Dooksbff78652009-07-18 10:12:28 +0100299
300 /* Register with the core ADC driver. */
301
302 hwmon->client = s3c_adc_register(dev, NULL, NULL, 0);
303 if (IS_ERR(hwmon->client)) {
304 dev_err(&dev->dev, "cannot register adc\n");
Sachin Kamat41bf8702012-07-13 14:36:28 +0530305 return PTR_ERR(hwmon->client);
Ben Dooksbff78652009-07-18 10:12:28 +0100306 }
307
308 /* add attributes for our adc devices. */
309
310 ret = s3c_hwmon_add_raw(&dev->dev);
311 if (ret)
312 goto err_registered;
313
314 /* register with the hwmon core */
315
316 hwmon->hwmon_dev = hwmon_device_register(&dev->dev);
317 if (IS_ERR(hwmon->hwmon_dev)) {
318 dev_err(&dev->dev, "error registering with hwmon\n");
319 ret = PTR_ERR(hwmon->hwmon_dev);
320 goto err_raw_attribute;
321 }
322
323 for (i = 0; i < ARRAY_SIZE(pdata->in); i++) {
Ben Dooks6d905e62009-12-09 20:35:45 +0100324 struct s3c_hwmon_chcfg *cfg = pdata->in[i];
Ben Dooksbe4c23c2009-11-16 12:45:39 +0100325
326 if (!cfg)
Ben Dooksbff78652009-07-18 10:12:28 +0100327 continue;
328
Ben Dooksbe4c23c2009-11-16 12:45:39 +0100329 if (cfg->mult >= 0x10000)
Ben Dooksbff78652009-07-18 10:12:28 +0100330 dev_warn(&dev->dev,
331 "channel %d multiplier too large\n",
332 i);
333
Ben Dooks6d905e62009-12-09 20:35:45 +0100334 if (cfg->div == 0) {
Ben Dooksbe4c23c2009-11-16 12:45:39 +0100335 dev_err(&dev->dev, "channel %d divider zero\n", i);
336 continue;
337 }
338
Ben Dooksbff78652009-07-18 10:12:28 +0100339 ret = s3c_hwmon_create_attr(&dev->dev, pdata->in[i],
340 &hwmon->attrs[i], i);
341 if (ret) {
342 dev_err(&dev->dev,
343 "error creating channel %d\n", i);
344
345 for (i--; i >= 0; i--)
346 s3c_hwmon_remove_attr(&dev->dev,
347 &hwmon->attrs[i]);
348
349 goto err_hwmon_register;
350 }
351 }
352
353 return 0;
354
355 err_hwmon_register:
356 hwmon_device_unregister(hwmon->hwmon_dev);
357
358 err_raw_attribute:
359 s3c_hwmon_remove_raw(&dev->dev);
360
361 err_registered:
362 s3c_adc_release(hwmon->client);
363
Ben Dooksbff78652009-07-18 10:12:28 +0100364 return ret;
365}
366
Bill Pemberton281dfd02012-11-19 13:25:51 -0500367static int s3c_hwmon_remove(struct platform_device *dev)
Ben Dooksbff78652009-07-18 10:12:28 +0100368{
369 struct s3c_hwmon *hwmon = platform_get_drvdata(dev);
370 int i;
371
372 s3c_hwmon_remove_raw(&dev->dev);
373
374 for (i = 0; i < ARRAY_SIZE(hwmon->attrs); i++)
375 s3c_hwmon_remove_attr(&dev->dev, &hwmon->attrs[i]);
376
377 hwmon_device_unregister(hwmon->hwmon_dev);
378 s3c_adc_release(hwmon->client);
379
380 return 0;
381}
382
383static struct platform_driver s3c_hwmon_driver = {
384 .driver = {
385 .name = "s3c-hwmon",
386 .owner = THIS_MODULE,
387 },
388 .probe = s3c_hwmon_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500389 .remove = s3c_hwmon_remove,
Ben Dooksbff78652009-07-18 10:12:28 +0100390};
391
Axel Lin25a236a2011-11-25 02:31:00 -0500392module_platform_driver(s3c_hwmon_driver);
Ben Dooksbff78652009-07-18 10:12:28 +0100393
394MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
395MODULE_DESCRIPTION("S3C ADC HWMon driver");
396MODULE_LICENSE("GPL v2");
397MODULE_ALIAS("platform:s3c-hwmon");