ad7418.c 8.1 KB

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