ad7418.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * An hwmon driver for the Analog Devices AD7416/17/18
  3. * Copyright (C) 2006-07 Tower Technologies
  4. *
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * Based on lm75.c
  8. * Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License,
  12. * as published by the Free Software Foundation - version 2.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/i2c.h>
  17. #include <linux/hwmon.h>
  18. #include <linux/hwmon-sysfs.h>
  19. #include <linux/err.h>
  20. #include <linux/mutex.h>
  21. #include <linux/delay.h>
  22. #include <linux/slab.h>
  23. #include "lm75.h"
  24. #define DRV_VERSION "0.4"
  25. enum chips { ad7416, ad7417, ad7418 };
  26. /* AD7418 registers */
  27. #define AD7418_REG_TEMP_IN 0x00
  28. #define AD7418_REG_CONF 0x01
  29. #define AD7418_REG_TEMP_HYST 0x02
  30. #define AD7418_REG_TEMP_OS 0x03
  31. #define AD7418_REG_ADC 0x04
  32. #define AD7418_REG_CONF2 0x05
  33. #define AD7418_REG_ADC_CH(x) ((x) << 5)
  34. #define AD7418_CH_TEMP AD7418_REG_ADC_CH(0)
  35. static const u8 AD7418_REG_TEMP[] = { AD7418_REG_TEMP_IN,
  36. AD7418_REG_TEMP_HYST,
  37. AD7418_REG_TEMP_OS };
  38. struct ad7418_data {
  39. struct device *hwmon_dev;
  40. struct attribute_group attrs;
  41. enum chips type;
  42. struct mutex lock;
  43. int adc_max; /* number of ADC channels */
  44. char valid;
  45. unsigned long last_updated; /* In jiffies */
  46. s16 temp[3]; /* Register values */
  47. u16 in[4];
  48. };
  49. static int ad7418_probe(struct i2c_client *client,
  50. const struct i2c_device_id *id);
  51. static int ad7418_remove(struct i2c_client *client);
  52. static const struct i2c_device_id ad7418_id[] = {
  53. { "ad7416", ad7416 },
  54. { "ad7417", ad7417 },
  55. { "ad7418", ad7418 },
  56. { }
  57. };
  58. MODULE_DEVICE_TABLE(i2c, ad7418_id);
  59. static struct i2c_driver ad7418_driver = {
  60. .driver = {
  61. .name = "ad7418",
  62. },
  63. .probe = ad7418_probe,
  64. .remove = ad7418_remove,
  65. .id_table = ad7418_id,
  66. };
  67. /* All registers are word-sized, except for the configuration registers.
  68. * AD7418 uses a high-byte first convention. Do NOT use those functions to
  69. * access the configuration registers CONF and CONF2, as they are byte-sized.
  70. */
  71. static inline int ad7418_read(struct i2c_client *client, u8 reg)
  72. {
  73. return swab16(i2c_smbus_read_word_data(client, reg));
  74. }
  75. static inline int ad7418_write(struct i2c_client *client, u8 reg, u16 value)
  76. {
  77. return i2c_smbus_write_word_data(client, reg, swab16(value));
  78. }
  79. static void ad7418_init_client(struct i2c_client *client)
  80. {
  81. struct ad7418_data *data = i2c_get_clientdata(client);
  82. int reg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
  83. if (reg < 0) {
  84. dev_err(&client->dev, "cannot read configuration register\n");
  85. } else {
  86. dev_info(&client->dev, "configuring for mode 1\n");
  87. i2c_smbus_write_byte_data(client, AD7418_REG_CONF, reg & 0xfe);
  88. if (data->type == ad7417 || data->type == ad7418)
  89. i2c_smbus_write_byte_data(client,
  90. AD7418_REG_CONF2, 0x00);
  91. }
  92. }
  93. static struct ad7418_data *ad7418_update_device(struct device *dev)
  94. {
  95. struct i2c_client *client = to_i2c_client(dev);
  96. struct ad7418_data *data = i2c_get_clientdata(client);
  97. mutex_lock(&data->lock);
  98. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  99. || !data->valid) {
  100. u8 cfg;
  101. int i, ch;
  102. /* read config register and clear channel bits */
  103. cfg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
  104. cfg &= 0x1F;
  105. i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
  106. cfg | AD7418_CH_TEMP);
  107. udelay(30);
  108. for (i = 0; i < 3; i++) {
  109. data->temp[i] = ad7418_read(client, AD7418_REG_TEMP[i]);
  110. }
  111. for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
  112. i2c_smbus_write_byte_data(client,
  113. AD7418_REG_CONF,
  114. cfg | AD7418_REG_ADC_CH(ch));
  115. udelay(15);
  116. data->in[data->adc_max - 1 - i] =
  117. ad7418_read(client, AD7418_REG_ADC);
  118. }
  119. /* restore old configuration value */
  120. ad7418_write(client, AD7418_REG_CONF, cfg);
  121. data->last_updated = jiffies;
  122. data->valid = 1;
  123. }
  124. mutex_unlock(&data->lock);
  125. return data;
  126. }
  127. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  128. char *buf)
  129. {
  130. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  131. struct ad7418_data *data = ad7418_update_device(dev);
  132. return sprintf(buf, "%d\n",
  133. LM75_TEMP_FROM_REG(data->temp[attr->index]));
  134. }
  135. static ssize_t show_adc(struct device *dev, struct device_attribute *devattr,
  136. char *buf)
  137. {
  138. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  139. struct ad7418_data *data = ad7418_update_device(dev);
  140. return sprintf(buf, "%d\n",
  141. ((data->in[attr->index] >> 6) * 2500 + 512) / 1024);
  142. }
  143. static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
  144. const char *buf, size_t count)
  145. {
  146. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  147. struct i2c_client *client = to_i2c_client(dev);
  148. struct ad7418_data *data = i2c_get_clientdata(client);
  149. long temp = simple_strtol(buf, NULL, 10);
  150. mutex_lock(&data->lock);
  151. data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
  152. ad7418_write(client, AD7418_REG_TEMP[attr->index], data->temp[attr->index]);
  153. mutex_unlock(&data->lock);
  154. return count;
  155. }
  156. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  157. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  158. show_temp, set_temp, 1);
  159. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  160. show_temp, set_temp, 2);
  161. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_adc, NULL, 0);
  162. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_adc, NULL, 1);
  163. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 2);
  164. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 3);
  165. static struct attribute *ad7416_attributes[] = {
  166. &sensor_dev_attr_temp1_max.dev_attr.attr,
  167. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  168. &sensor_dev_attr_temp1_input.dev_attr.attr,
  169. NULL
  170. };
  171. static struct attribute *ad7417_attributes[] = {
  172. &sensor_dev_attr_temp1_max.dev_attr.attr,
  173. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  174. &sensor_dev_attr_temp1_input.dev_attr.attr,
  175. &sensor_dev_attr_in1_input.dev_attr.attr,
  176. &sensor_dev_attr_in2_input.dev_attr.attr,
  177. &sensor_dev_attr_in3_input.dev_attr.attr,
  178. &sensor_dev_attr_in4_input.dev_attr.attr,
  179. NULL
  180. };
  181. static struct attribute *ad7418_attributes[] = {
  182. &sensor_dev_attr_temp1_max.dev_attr.attr,
  183. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  184. &sensor_dev_attr_temp1_input.dev_attr.attr,
  185. &sensor_dev_attr_in1_input.dev_attr.attr,
  186. NULL
  187. };
  188. static int ad7418_probe(struct i2c_client *client,
  189. const struct i2c_device_id *id)
  190. {
  191. struct i2c_adapter *adapter = client->adapter;
  192. struct ad7418_data *data;
  193. int err;
  194. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  195. I2C_FUNC_SMBUS_WORD_DATA)) {
  196. err = -EOPNOTSUPP;
  197. goto exit;
  198. }
  199. if (!(data = kzalloc(sizeof(struct ad7418_data), GFP_KERNEL))) {
  200. err = -ENOMEM;
  201. goto exit;
  202. }
  203. i2c_set_clientdata(client, data);
  204. mutex_init(&data->lock);
  205. data->type = id->driver_data;
  206. switch (data->type) {
  207. case ad7416:
  208. data->adc_max = 0;
  209. data->attrs.attrs = ad7416_attributes;
  210. break;
  211. case ad7417:
  212. data->adc_max = 4;
  213. data->attrs.attrs = ad7417_attributes;
  214. break;
  215. case ad7418:
  216. data->adc_max = 1;
  217. data->attrs.attrs = ad7418_attributes;
  218. break;
  219. }
  220. dev_info(&client->dev, "%s chip found\n", client->name);
  221. /* Initialize the AD7418 chip */
  222. ad7418_init_client(client);
  223. /* Register sysfs hooks */
  224. if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
  225. goto exit_free;
  226. data->hwmon_dev = hwmon_device_register(&client->dev);
  227. if (IS_ERR(data->hwmon_dev)) {
  228. err = PTR_ERR(data->hwmon_dev);
  229. goto exit_remove;
  230. }
  231. return 0;
  232. exit_remove:
  233. sysfs_remove_group(&client->dev.kobj, &data->attrs);
  234. exit_free:
  235. kfree(data);
  236. exit:
  237. return err;
  238. }
  239. static int ad7418_remove(struct i2c_client *client)
  240. {
  241. struct ad7418_data *data = i2c_get_clientdata(client);
  242. hwmon_device_unregister(data->hwmon_dev);
  243. sysfs_remove_group(&client->dev.kobj, &data->attrs);
  244. kfree(data);
  245. return 0;
  246. }
  247. static int __init ad7418_init(void)
  248. {
  249. return i2c_add_driver(&ad7418_driver);
  250. }
  251. static void __exit ad7418_exit(void)
  252. {
  253. i2c_del_driver(&ad7418_driver);
  254. }
  255. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  256. MODULE_DESCRIPTION("AD7416/17/18 driver");
  257. MODULE_LICENSE("GPL");
  258. MODULE_VERSION(DRV_VERSION);
  259. module_init(ad7418_init);
  260. module_exit(ad7418_exit);