ad7418.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. static void ad7418_init_client(struct i2c_client *client)
  68. {
  69. struct ad7418_data *data = i2c_get_clientdata(client);
  70. int reg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
  71. if (reg < 0) {
  72. dev_err(&client->dev, "cannot read configuration register\n");
  73. } else {
  74. dev_info(&client->dev, "configuring for mode 1\n");
  75. i2c_smbus_write_byte_data(client, AD7418_REG_CONF, reg & 0xfe);
  76. if (data->type == ad7417 || data->type == ad7418)
  77. i2c_smbus_write_byte_data(client,
  78. AD7418_REG_CONF2, 0x00);
  79. }
  80. }
  81. static struct ad7418_data *ad7418_update_device(struct device *dev)
  82. {
  83. struct i2c_client *client = to_i2c_client(dev);
  84. struct ad7418_data *data = i2c_get_clientdata(client);
  85. mutex_lock(&data->lock);
  86. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  87. || !data->valid) {
  88. u8 cfg;
  89. int i, ch;
  90. /* read config register and clear channel bits */
  91. cfg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
  92. cfg &= 0x1F;
  93. i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
  94. cfg | AD7418_CH_TEMP);
  95. udelay(30);
  96. for (i = 0; i < 3; i++) {
  97. data->temp[i] =
  98. i2c_smbus_read_word_swapped(client,
  99. AD7418_REG_TEMP[i]);
  100. }
  101. for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
  102. i2c_smbus_write_byte_data(client,
  103. AD7418_REG_CONF,
  104. cfg | AD7418_REG_ADC_CH(ch));
  105. udelay(15);
  106. data->in[data->adc_max - 1 - i] =
  107. i2c_smbus_read_word_swapped(client,
  108. AD7418_REG_ADC);
  109. }
  110. /* restore old configuration value */
  111. i2c_smbus_write_word_swapped(client, AD7418_REG_CONF, cfg);
  112. data->last_updated = jiffies;
  113. data->valid = 1;
  114. }
  115. mutex_unlock(&data->lock);
  116. return data;
  117. }
  118. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  119. char *buf)
  120. {
  121. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  122. struct ad7418_data *data = ad7418_update_device(dev);
  123. return sprintf(buf, "%d\n",
  124. LM75_TEMP_FROM_REG(data->temp[attr->index]));
  125. }
  126. static ssize_t show_adc(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. ((data->in[attr->index] >> 6) * 2500 + 512) / 1024);
  133. }
  134. static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
  135. const char *buf, size_t count)
  136. {
  137. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  138. struct i2c_client *client = to_i2c_client(dev);
  139. struct ad7418_data *data = i2c_get_clientdata(client);
  140. long temp = simple_strtol(buf, NULL, 10);
  141. mutex_lock(&data->lock);
  142. data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
  143. i2c_smbus_write_word_swapped(client,
  144. AD7418_REG_TEMP[attr->index],
  145. data->temp[attr->index]);
  146. mutex_unlock(&data->lock);
  147. return count;
  148. }
  149. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  150. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  151. show_temp, set_temp, 1);
  152. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  153. show_temp, set_temp, 2);
  154. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_adc, NULL, 0);
  155. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_adc, NULL, 1);
  156. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 2);
  157. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 3);
  158. static struct attribute *ad7416_attributes[] = {
  159. &sensor_dev_attr_temp1_max.dev_attr.attr,
  160. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  161. &sensor_dev_attr_temp1_input.dev_attr.attr,
  162. NULL
  163. };
  164. static struct attribute *ad7417_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. &sensor_dev_attr_in1_input.dev_attr.attr,
  169. &sensor_dev_attr_in2_input.dev_attr.attr,
  170. &sensor_dev_attr_in3_input.dev_attr.attr,
  171. &sensor_dev_attr_in4_input.dev_attr.attr,
  172. NULL
  173. };
  174. static struct attribute *ad7418_attributes[] = {
  175. &sensor_dev_attr_temp1_max.dev_attr.attr,
  176. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  177. &sensor_dev_attr_temp1_input.dev_attr.attr,
  178. &sensor_dev_attr_in1_input.dev_attr.attr,
  179. NULL
  180. };
  181. static int ad7418_probe(struct i2c_client *client,
  182. const struct i2c_device_id *id)
  183. {
  184. struct i2c_adapter *adapter = client->adapter;
  185. struct ad7418_data *data;
  186. int err;
  187. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  188. I2C_FUNC_SMBUS_WORD_DATA)) {
  189. err = -EOPNOTSUPP;
  190. goto exit;
  191. }
  192. if (!(data = kzalloc(sizeof(struct ad7418_data), GFP_KERNEL))) {
  193. err = -ENOMEM;
  194. goto exit;
  195. }
  196. i2c_set_clientdata(client, data);
  197. mutex_init(&data->lock);
  198. data->type = id->driver_data;
  199. switch (data->type) {
  200. case ad7416:
  201. data->adc_max = 0;
  202. data->attrs.attrs = ad7416_attributes;
  203. break;
  204. case ad7417:
  205. data->adc_max = 4;
  206. data->attrs.attrs = ad7417_attributes;
  207. break;
  208. case ad7418:
  209. data->adc_max = 1;
  210. data->attrs.attrs = ad7418_attributes;
  211. break;
  212. }
  213. dev_info(&client->dev, "%s chip found\n", client->name);
  214. /* Initialize the AD7418 chip */
  215. ad7418_init_client(client);
  216. /* Register sysfs hooks */
  217. if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
  218. goto exit_free;
  219. data->hwmon_dev = hwmon_device_register(&client->dev);
  220. if (IS_ERR(data->hwmon_dev)) {
  221. err = PTR_ERR(data->hwmon_dev);
  222. goto exit_remove;
  223. }
  224. return 0;
  225. exit_remove:
  226. sysfs_remove_group(&client->dev.kobj, &data->attrs);
  227. exit_free:
  228. kfree(data);
  229. exit:
  230. return err;
  231. }
  232. static int ad7418_remove(struct i2c_client *client)
  233. {
  234. struct ad7418_data *data = i2c_get_clientdata(client);
  235. hwmon_device_unregister(data->hwmon_dev);
  236. sysfs_remove_group(&client->dev.kobj, &data->attrs);
  237. kfree(data);
  238. return 0;
  239. }
  240. static int __init ad7418_init(void)
  241. {
  242. return i2c_add_driver(&ad7418_driver);
  243. }
  244. static void __exit ad7418_exit(void)
  245. {
  246. i2c_del_driver(&ad7418_driver);
  247. }
  248. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  249. MODULE_DESCRIPTION("AD7416/17/18 driver");
  250. MODULE_LICENSE("GPL");
  251. MODULE_VERSION(DRV_VERSION);
  252. module_init(ad7418_init);
  253. module_exit(ad7418_exit);