ad7418.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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;
  141. int ret = kstrtol(buf, 10, &temp);
  142. if (ret < 0)
  143. return ret;
  144. mutex_lock(&data->lock);
  145. data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
  146. i2c_smbus_write_word_swapped(client,
  147. AD7418_REG_TEMP[attr->index],
  148. data->temp[attr->index]);
  149. mutex_unlock(&data->lock);
  150. return count;
  151. }
  152. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  153. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  154. show_temp, set_temp, 1);
  155. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  156. show_temp, set_temp, 2);
  157. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_adc, NULL, 0);
  158. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_adc, NULL, 1);
  159. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 2);
  160. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 3);
  161. static struct attribute *ad7416_attributes[] = {
  162. &sensor_dev_attr_temp1_max.dev_attr.attr,
  163. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  164. &sensor_dev_attr_temp1_input.dev_attr.attr,
  165. NULL
  166. };
  167. static struct attribute *ad7417_attributes[] = {
  168. &sensor_dev_attr_temp1_max.dev_attr.attr,
  169. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  170. &sensor_dev_attr_temp1_input.dev_attr.attr,
  171. &sensor_dev_attr_in1_input.dev_attr.attr,
  172. &sensor_dev_attr_in2_input.dev_attr.attr,
  173. &sensor_dev_attr_in3_input.dev_attr.attr,
  174. &sensor_dev_attr_in4_input.dev_attr.attr,
  175. NULL
  176. };
  177. static struct attribute *ad7418_attributes[] = {
  178. &sensor_dev_attr_temp1_max.dev_attr.attr,
  179. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  180. &sensor_dev_attr_temp1_input.dev_attr.attr,
  181. &sensor_dev_attr_in1_input.dev_attr.attr,
  182. NULL
  183. };
  184. static int ad7418_probe(struct i2c_client *client,
  185. const struct i2c_device_id *id)
  186. {
  187. struct i2c_adapter *adapter = client->adapter;
  188. struct ad7418_data *data;
  189. int err;
  190. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  191. I2C_FUNC_SMBUS_WORD_DATA)) {
  192. err = -EOPNOTSUPP;
  193. goto exit;
  194. }
  195. data = kzalloc(sizeof(struct ad7418_data), GFP_KERNEL);
  196. if (!data) {
  197. err = -ENOMEM;
  198. goto exit;
  199. }
  200. i2c_set_clientdata(client, data);
  201. mutex_init(&data->lock);
  202. data->type = id->driver_data;
  203. switch (data->type) {
  204. case ad7416:
  205. data->adc_max = 0;
  206. data->attrs.attrs = ad7416_attributes;
  207. break;
  208. case ad7417:
  209. data->adc_max = 4;
  210. data->attrs.attrs = ad7417_attributes;
  211. break;
  212. case ad7418:
  213. data->adc_max = 1;
  214. data->attrs.attrs = ad7418_attributes;
  215. break;
  216. }
  217. dev_info(&client->dev, "%s chip found\n", client->name);
  218. /* Initialize the AD7418 chip */
  219. ad7418_init_client(client);
  220. /* Register sysfs hooks */
  221. err = sysfs_create_group(&client->dev.kobj, &data->attrs);
  222. if (err)
  223. goto exit_free;
  224. data->hwmon_dev = hwmon_device_register(&client->dev);
  225. if (IS_ERR(data->hwmon_dev)) {
  226. err = PTR_ERR(data->hwmon_dev);
  227. goto exit_remove;
  228. }
  229. return 0;
  230. exit_remove:
  231. sysfs_remove_group(&client->dev.kobj, &data->attrs);
  232. exit_free:
  233. kfree(data);
  234. exit:
  235. return err;
  236. }
  237. static int ad7418_remove(struct i2c_client *client)
  238. {
  239. struct ad7418_data *data = i2c_get_clientdata(client);
  240. hwmon_device_unregister(data->hwmon_dev);
  241. sysfs_remove_group(&client->dev.kobj, &data->attrs);
  242. kfree(data);
  243. return 0;
  244. }
  245. module_i2c_driver(ad7418_driver);
  246. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  247. MODULE_DESCRIPTION("AD7416/17/18 driver");
  248. MODULE_LICENSE("GPL");
  249. MODULE_VERSION(DRV_VERSION);