Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 1 | /* 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 Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 25 | #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 Bergmann | 436d42c | 2012-08-24 15:22:12 +0200 | [diff] [blame] | 36 | #include <linux/platform_data/hwmon-s3c.h> |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 37 | |
| 38 | struct 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 | */ |
| 52 | struct s3c_hwmon { |
Thomas Gleixner | 9c627e3 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 53 | struct mutex lock; |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 54 | 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 | */ |
| 70 | static int s3c_hwmon_read_ch(struct device *dev, |
| 71 | struct s3c_hwmon *hwmon, int channel) |
| 72 | { |
| 73 | int ret; |
| 74 | |
Thomas Gleixner | 9c627e3 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 75 | ret = mutex_lock_interruptible(&hwmon->lock); |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 76 | 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 Gleixner | 9c627e3 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 82 | mutex_unlock(&hwmon->lock); |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 83 | |
| 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 | */ |
| 98 | static 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 | |
Guenter Roeck | 24f9c53 | 2013-01-10 05:54:40 -0800 | [diff] [blame] | 110 | static SENSOR_DEVICE_ATTR(adc0_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 0); |
| 111 | static SENSOR_DEVICE_ATTR(adc1_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 1); |
| 112 | static SENSOR_DEVICE_ATTR(adc2_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 2); |
| 113 | static SENSOR_DEVICE_ATTR(adc3_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 3); |
| 114 | static SENSOR_DEVICE_ATTR(adc4_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 4); |
| 115 | static SENSOR_DEVICE_ATTR(adc5_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 5); |
| 116 | static SENSOR_DEVICE_ATTR(adc6_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 6); |
| 117 | static SENSOR_DEVICE_ATTR(adc7_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 7); |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 118 | |
| 119 | static struct attribute *s3c_hwmon_attrs[9] = { |
| 120 | &sensor_dev_attr_adc0_raw.dev_attr.attr, |
| 121 | &sensor_dev_attr_adc1_raw.dev_attr.attr, |
| 122 | &sensor_dev_attr_adc2_raw.dev_attr.attr, |
| 123 | &sensor_dev_attr_adc3_raw.dev_attr.attr, |
| 124 | &sensor_dev_attr_adc4_raw.dev_attr.attr, |
| 125 | &sensor_dev_attr_adc5_raw.dev_attr.attr, |
| 126 | &sensor_dev_attr_adc6_raw.dev_attr.attr, |
| 127 | &sensor_dev_attr_adc7_raw.dev_attr.attr, |
| 128 | NULL, |
| 129 | }; |
| 130 | |
| 131 | static struct attribute_group s3c_hwmon_attrgroup = { |
| 132 | .attrs = s3c_hwmon_attrs, |
| 133 | }; |
| 134 | |
| 135 | static inline int s3c_hwmon_add_raw(struct device *dev) |
| 136 | { |
| 137 | return sysfs_create_group(&dev->kobj, &s3c_hwmon_attrgroup); |
| 138 | } |
| 139 | |
| 140 | static inline void s3c_hwmon_remove_raw(struct device *dev) |
| 141 | { |
| 142 | sysfs_remove_group(&dev->kobj, &s3c_hwmon_attrgroup); |
| 143 | } |
| 144 | |
| 145 | #else |
| 146 | |
| 147 | static inline int s3c_hwmon_add_raw(struct device *dev) { return 0; } |
| 148 | static inline void s3c_hwmon_remove_raw(struct device *dev) { } |
| 149 | |
| 150 | #endif /* CONFIG_SENSORS_S3C_RAW */ |
| 151 | |
| 152 | /** |
| 153 | * s3c_hwmon_ch_show - show value of a given channel |
| 154 | * @dev: The device that the attribute belongs to. |
| 155 | * @attr: The attribute being read. |
| 156 | * @buf: The result buffer. |
| 157 | * |
| 158 | * Read a value from the ADC and scale it before returning it to the |
| 159 | * caller. The scale factor is gained from the channel configuration |
| 160 | * passed via the platform data when the device was registered. |
| 161 | */ |
| 162 | static ssize_t s3c_hwmon_ch_show(struct device *dev, |
| 163 | struct device_attribute *attr, |
| 164 | char *buf) |
| 165 | { |
| 166 | struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr); |
| 167 | struct s3c_hwmon *hwmon = platform_get_drvdata(to_platform_device(dev)); |
Jingoo Han | a8b3a3a | 2013-07-30 17:13:06 +0900 | [diff] [blame] | 168 | struct s3c_hwmon_pdata *pdata = dev_get_platdata(dev); |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 169 | struct s3c_hwmon_chcfg *cfg; |
| 170 | int ret; |
| 171 | |
| 172 | cfg = pdata->in[sen_attr->index]; |
| 173 | |
| 174 | ret = s3c_hwmon_read_ch(dev, hwmon, sen_attr->index); |
| 175 | if (ret < 0) |
| 176 | return ret; |
| 177 | |
| 178 | ret *= cfg->mult; |
| 179 | ret = DIV_ROUND_CLOSEST(ret, cfg->div); |
| 180 | |
| 181 | return snprintf(buf, PAGE_SIZE, "%d\n", ret); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * s3c_hwmon_label_show - show label name of the given channel. |
| 186 | * @dev: The device that the attribute belongs to. |
| 187 | * @attr: The attribute being read. |
| 188 | * @buf: The result buffer. |
| 189 | * |
| 190 | * Return the label name of a given channel |
| 191 | */ |
| 192 | static ssize_t s3c_hwmon_label_show(struct device *dev, |
| 193 | struct device_attribute *attr, |
| 194 | char *buf) |
| 195 | { |
| 196 | struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr); |
Jingoo Han | a8b3a3a | 2013-07-30 17:13:06 +0900 | [diff] [blame] | 197 | struct s3c_hwmon_pdata *pdata = dev_get_platdata(dev); |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 198 | struct s3c_hwmon_chcfg *cfg; |
| 199 | |
| 200 | cfg = pdata->in[sen_attr->index]; |
| 201 | |
| 202 | return snprintf(buf, PAGE_SIZE, "%s\n", cfg->name); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * s3c_hwmon_create_attr - create hwmon attribute for given channel. |
| 207 | * @dev: The device to create the attribute on. |
| 208 | * @cfg: The channel configuration passed from the platform data. |
| 209 | * @channel: The ADC channel number to process. |
| 210 | * |
| 211 | * Create the scaled attribute for use with hwmon from the specified |
| 212 | * platform data in @pdata. The sysfs entry is handled by the routine |
| 213 | * s3c_hwmon_ch_show(). |
| 214 | * |
| 215 | * The attribute name is taken from the configuration data if present |
| 216 | * otherwise the name is taken by concatenating in_ with the channel |
| 217 | * number. |
| 218 | */ |
| 219 | static int s3c_hwmon_create_attr(struct device *dev, |
| 220 | struct s3c_hwmon_chcfg *cfg, |
| 221 | struct s3c_hwmon_attr *attrs, |
| 222 | int channel) |
| 223 | { |
| 224 | struct sensor_device_attribute *attr; |
| 225 | int ret; |
| 226 | |
| 227 | snprintf(attrs->in_name, sizeof(attrs->in_name), "in%d_input", channel); |
| 228 | |
| 229 | attr = &attrs->in; |
| 230 | attr->index = channel; |
Guenter Roeck | b1e698d | 2011-05-24 12:34:55 -0700 | [diff] [blame] | 231 | sysfs_attr_init(&attr->dev_attr.attr); |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 232 | attr->dev_attr.attr.name = attrs->in_name; |
| 233 | attr->dev_attr.attr.mode = S_IRUGO; |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 234 | attr->dev_attr.show = s3c_hwmon_ch_show; |
| 235 | |
| 236 | ret = device_create_file(dev, &attr->dev_attr); |
| 237 | if (ret < 0) { |
| 238 | dev_err(dev, "failed to create input attribute\n"); |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | /* if this has a name, add a label */ |
| 243 | if (cfg->name) { |
| 244 | snprintf(attrs->label_name, sizeof(attrs->label_name), |
| 245 | "in%d_label", channel); |
| 246 | |
| 247 | attr = &attrs->label; |
| 248 | attr->index = channel; |
Guenter Roeck | b1e698d | 2011-05-24 12:34:55 -0700 | [diff] [blame] | 249 | sysfs_attr_init(&attr->dev_attr.attr); |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 250 | attr->dev_attr.attr.name = attrs->label_name; |
| 251 | attr->dev_attr.attr.mode = S_IRUGO; |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 252 | attr->dev_attr.show = s3c_hwmon_label_show; |
| 253 | |
| 254 | ret = device_create_file(dev, &attr->dev_attr); |
| 255 | if (ret < 0) { |
| 256 | device_remove_file(dev, &attrs->in.dev_attr); |
| 257 | dev_err(dev, "failed to create label attribute\n"); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static void s3c_hwmon_remove_attr(struct device *dev, |
| 265 | struct s3c_hwmon_attr *attrs) |
| 266 | { |
| 267 | device_remove_file(dev, &attrs->in.dev_attr); |
| 268 | device_remove_file(dev, &attrs->label.dev_attr); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * s3c_hwmon_probe - device probe entry. |
| 273 | * @dev: The device being probed. |
| 274 | */ |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 275 | static int s3c_hwmon_probe(struct platform_device *dev) |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 276 | { |
Jingoo Han | a8b3a3a | 2013-07-30 17:13:06 +0900 | [diff] [blame] | 277 | struct s3c_hwmon_pdata *pdata = dev_get_platdata(&dev->dev); |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 278 | struct s3c_hwmon *hwmon; |
| 279 | int ret = 0; |
| 280 | int i; |
| 281 | |
| 282 | if (!pdata) { |
| 283 | dev_err(&dev->dev, "no platform data supplied\n"); |
| 284 | return -EINVAL; |
| 285 | } |
| 286 | |
Sachin Kamat | 41bf870 | 2012-07-13 14:36:28 +0530 | [diff] [blame] | 287 | hwmon = devm_kzalloc(&dev->dev, sizeof(struct s3c_hwmon), GFP_KERNEL); |
Jingoo Han | eefb6b7 | 2014-04-29 17:12:25 +0900 | [diff] [blame] | 288 | if (hwmon == NULL) |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 289 | return -ENOMEM; |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 290 | |
| 291 | platform_set_drvdata(dev, hwmon); |
| 292 | |
Thomas Gleixner | 9c627e3 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 293 | mutex_init(&hwmon->lock); |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 294 | |
| 295 | /* Register with the core ADC driver. */ |
| 296 | |
| 297 | hwmon->client = s3c_adc_register(dev, NULL, NULL, 0); |
| 298 | if (IS_ERR(hwmon->client)) { |
| 299 | dev_err(&dev->dev, "cannot register adc\n"); |
Sachin Kamat | 41bf870 | 2012-07-13 14:36:28 +0530 | [diff] [blame] | 300 | return PTR_ERR(hwmon->client); |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* add attributes for our adc devices. */ |
| 304 | |
| 305 | ret = s3c_hwmon_add_raw(&dev->dev); |
| 306 | if (ret) |
| 307 | goto err_registered; |
| 308 | |
| 309 | /* register with the hwmon core */ |
| 310 | |
| 311 | hwmon->hwmon_dev = hwmon_device_register(&dev->dev); |
| 312 | if (IS_ERR(hwmon->hwmon_dev)) { |
| 313 | dev_err(&dev->dev, "error registering with hwmon\n"); |
| 314 | ret = PTR_ERR(hwmon->hwmon_dev); |
| 315 | goto err_raw_attribute; |
| 316 | } |
| 317 | |
| 318 | for (i = 0; i < ARRAY_SIZE(pdata->in); i++) { |
Ben Dooks | 6d905e6 | 2009-12-09 20:35:45 +0100 | [diff] [blame] | 319 | struct s3c_hwmon_chcfg *cfg = pdata->in[i]; |
Ben Dooks | be4c23c | 2009-11-16 12:45:39 +0100 | [diff] [blame] | 320 | |
| 321 | if (!cfg) |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 322 | continue; |
| 323 | |
Ben Dooks | be4c23c | 2009-11-16 12:45:39 +0100 | [diff] [blame] | 324 | if (cfg->mult >= 0x10000) |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 325 | dev_warn(&dev->dev, |
| 326 | "channel %d multiplier too large\n", |
| 327 | i); |
| 328 | |
Ben Dooks | 6d905e6 | 2009-12-09 20:35:45 +0100 | [diff] [blame] | 329 | if (cfg->div == 0) { |
Ben Dooks | be4c23c | 2009-11-16 12:45:39 +0100 | [diff] [blame] | 330 | dev_err(&dev->dev, "channel %d divider zero\n", i); |
| 331 | continue; |
| 332 | } |
| 333 | |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 334 | ret = s3c_hwmon_create_attr(&dev->dev, pdata->in[i], |
| 335 | &hwmon->attrs[i], i); |
| 336 | if (ret) { |
| 337 | dev_err(&dev->dev, |
| 338 | "error creating channel %d\n", i); |
| 339 | |
| 340 | for (i--; i >= 0; i--) |
| 341 | s3c_hwmon_remove_attr(&dev->dev, |
| 342 | &hwmon->attrs[i]); |
| 343 | |
| 344 | goto err_hwmon_register; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | |
| 350 | err_hwmon_register: |
| 351 | hwmon_device_unregister(hwmon->hwmon_dev); |
| 352 | |
| 353 | err_raw_attribute: |
| 354 | s3c_hwmon_remove_raw(&dev->dev); |
| 355 | |
| 356 | err_registered: |
| 357 | s3c_adc_release(hwmon->client); |
| 358 | |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 359 | return ret; |
| 360 | } |
| 361 | |
Bill Pemberton | 281dfd0 | 2012-11-19 13:25:51 -0500 | [diff] [blame] | 362 | static int s3c_hwmon_remove(struct platform_device *dev) |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 363 | { |
| 364 | struct s3c_hwmon *hwmon = platform_get_drvdata(dev); |
| 365 | int i; |
| 366 | |
| 367 | s3c_hwmon_remove_raw(&dev->dev); |
| 368 | |
| 369 | for (i = 0; i < ARRAY_SIZE(hwmon->attrs); i++) |
| 370 | s3c_hwmon_remove_attr(&dev->dev, &hwmon->attrs[i]); |
| 371 | |
| 372 | hwmon_device_unregister(hwmon->hwmon_dev); |
| 373 | s3c_adc_release(hwmon->client); |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static struct platform_driver s3c_hwmon_driver = { |
| 379 | .driver = { |
| 380 | .name = "s3c-hwmon", |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 381 | }, |
| 382 | .probe = s3c_hwmon_probe, |
Bill Pemberton | 9e5e9b7 | 2012-11-19 13:21:20 -0500 | [diff] [blame] | 383 | .remove = s3c_hwmon_remove, |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 384 | }; |
| 385 | |
Axel Lin | 25a236a | 2011-11-25 02:31:00 -0500 | [diff] [blame] | 386 | module_platform_driver(s3c_hwmon_driver); |
Ben Dooks | bff7865 | 2009-07-18 10:12:28 +0100 | [diff] [blame] | 387 | |
| 388 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); |
| 389 | MODULE_DESCRIPTION("S3C ADC HWMon driver"); |
| 390 | MODULE_LICENSE("GPL v2"); |
| 391 | MODULE_ALIAS("platform:s3c-hwmon"); |