ad7418.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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.3"
  24. /* Addresses to scan */
  25. static unsigned short normal_i2c[] = { 0x28, I2C_CLIENT_END };
  26. /* Insmod parameters */
  27. I2C_CLIENT_INSMOD_3(ad7416, ad7417, ad7418);
  28. /* AD7418 registers */
  29. #define AD7418_REG_TEMP_IN 0x00
  30. #define AD7418_REG_CONF 0x01
  31. #define AD7418_REG_TEMP_HYST 0x02
  32. #define AD7418_REG_TEMP_OS 0x03
  33. #define AD7418_REG_ADC 0x04
  34. #define AD7418_REG_CONF2 0x05
  35. #define AD7418_REG_ADC_CH(x) ((x) << 5)
  36. #define AD7418_CH_TEMP AD7418_REG_ADC_CH(0)
  37. static const u8 AD7418_REG_TEMP[] = { AD7418_REG_TEMP_IN,
  38. AD7418_REG_TEMP_HYST,
  39. AD7418_REG_TEMP_OS };
  40. struct ad7418_data {
  41. struct i2c_client client;
  42. struct class_device *class_dev;
  43. struct attribute_group attrs;
  44. enum chips type;
  45. struct mutex lock;
  46. int adc_max; /* number of ADC channels */
  47. char valid;
  48. unsigned long last_updated; /* In jiffies */
  49. s16 temp[3]; /* Register values */
  50. u16 in[4];
  51. };
  52. static int ad7418_attach_adapter(struct i2c_adapter *adapter);
  53. static int ad7418_detect(struct i2c_adapter *adapter, int address, int kind);
  54. static int ad7418_detach_client(struct i2c_client *client);
  55. static struct i2c_driver ad7418_driver = {
  56. .driver = {
  57. .name = "ad7418",
  58. },
  59. .attach_adapter = ad7418_attach_adapter,
  60. .detach_client = ad7418_detach_client,
  61. };
  62. /* All registers are word-sized, except for the configuration registers.
  63. * AD7418 uses a high-byte first convention. Do NOT use those functions to
  64. * access the configuration registers CONF and CONF2, as they are byte-sized.
  65. */
  66. static inline int ad7418_read(struct i2c_client *client, u8 reg)
  67. {
  68. return swab16(i2c_smbus_read_word_data(client, reg));
  69. }
  70. static inline int ad7418_write(struct i2c_client *client, u8 reg, u16 value)
  71. {
  72. return i2c_smbus_write_word_data(client, reg, swab16(value));
  73. }
  74. static void ad7418_init_client(struct i2c_client *client)
  75. {
  76. struct ad7418_data *data = i2c_get_clientdata(client);
  77. int reg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
  78. if (reg < 0) {
  79. dev_err(&client->dev, "cannot read configuration register\n");
  80. } else {
  81. dev_info(&client->dev, "configuring for mode 1\n");
  82. i2c_smbus_write_byte_data(client, AD7418_REG_CONF, reg & 0xfe);
  83. if (data->type == ad7417 || data->type == ad7418)
  84. i2c_smbus_write_byte_data(client,
  85. AD7418_REG_CONF2, 0x00);
  86. }
  87. }
  88. static struct ad7418_data *ad7418_update_device(struct device *dev)
  89. {
  90. struct i2c_client *client = to_i2c_client(dev);
  91. struct ad7418_data *data = i2c_get_clientdata(client);
  92. mutex_lock(&data->lock);
  93. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  94. || !data->valid) {
  95. u8 cfg;
  96. int i, ch;
  97. /* read config register and clear channel bits */
  98. cfg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
  99. cfg &= 0x1F;
  100. i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
  101. cfg | AD7418_CH_TEMP);
  102. udelay(30);
  103. for (i = 0; i < 3; i++) {
  104. data->temp[i] = ad7418_read(client, AD7418_REG_TEMP[i]);
  105. }
  106. for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
  107. i2c_smbus_write_byte_data(client,
  108. AD7418_REG_CONF,
  109. cfg | AD7418_REG_ADC_CH(ch));
  110. udelay(15);
  111. data->in[data->adc_max - 1 - i] =
  112. ad7418_read(client, AD7418_REG_ADC);
  113. }
  114. /* restore old configuration value */
  115. ad7418_write(client, AD7418_REG_CONF, cfg);
  116. data->last_updated = jiffies;
  117. data->valid = 1;
  118. }
  119. mutex_unlock(&data->lock);
  120. return data;
  121. }
  122. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  123. char *buf)
  124. {
  125. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  126. struct ad7418_data *data = ad7418_update_device(dev);
  127. return sprintf(buf, "%d\n",
  128. LM75_TEMP_FROM_REG(data->temp[attr->index]));
  129. }
  130. static ssize_t show_adc(struct device *dev, struct device_attribute *devattr,
  131. char *buf)
  132. {
  133. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  134. struct ad7418_data *data = ad7418_update_device(dev);
  135. return sprintf(buf, "%d\n",
  136. ((data->in[attr->index] >> 6) * 2500 + 512) / 1024);
  137. }
  138. static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
  139. const char *buf, size_t count)
  140. {
  141. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  142. struct i2c_client *client = to_i2c_client(dev);
  143. struct ad7418_data *data = i2c_get_clientdata(client);
  144. int temp = simple_strtol(buf, NULL, 10);
  145. mutex_lock(&data->lock);
  146. data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
  147. ad7418_write(client, AD7418_REG_TEMP[attr->index], data->temp[attr->index]);
  148. mutex_unlock(&data->lock);
  149. return count;
  150. }
  151. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  152. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  153. show_temp, set_temp, 1);
  154. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  155. show_temp, set_temp, 2);
  156. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_adc, NULL, 0);
  157. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_adc, NULL, 1);
  158. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 2);
  159. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 3);
  160. static int ad7418_attach_adapter(struct i2c_adapter *adapter)
  161. {
  162. if (!(adapter->class & I2C_CLASS_HWMON))
  163. return 0;
  164. return i2c_probe(adapter, &addr_data, ad7418_detect);
  165. }
  166. static struct attribute *ad7416_attributes[] = {
  167. &sensor_dev_attr_temp1_max.dev_attr.attr,
  168. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  169. &sensor_dev_attr_temp1_input.dev_attr.attr,
  170. NULL
  171. };
  172. static struct attribute *ad7417_attributes[] = {
  173. &sensor_dev_attr_temp1_max.dev_attr.attr,
  174. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  175. &sensor_dev_attr_temp1_input.dev_attr.attr,
  176. &sensor_dev_attr_in1_input.dev_attr.attr,
  177. &sensor_dev_attr_in2_input.dev_attr.attr,
  178. &sensor_dev_attr_in3_input.dev_attr.attr,
  179. &sensor_dev_attr_in4_input.dev_attr.attr,
  180. NULL
  181. };
  182. static struct attribute *ad7418_attributes[] = {
  183. &sensor_dev_attr_temp1_max.dev_attr.attr,
  184. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  185. &sensor_dev_attr_temp1_input.dev_attr.attr,
  186. &sensor_dev_attr_in1_input.dev_attr.attr,
  187. NULL
  188. };
  189. static int ad7418_detect(struct i2c_adapter *adapter, int address, int kind)
  190. {
  191. struct i2c_client *client;
  192. struct ad7418_data *data;
  193. int err = 0;
  194. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  195. I2C_FUNC_SMBUS_WORD_DATA))
  196. goto exit;
  197. if (!(data = kzalloc(sizeof(struct ad7418_data), GFP_KERNEL))) {
  198. err = -ENOMEM;
  199. goto exit;
  200. }
  201. client = &data->client;
  202. client->addr = address;
  203. client->adapter = adapter;
  204. client->driver = &ad7418_driver;
  205. i2c_set_clientdata(client, data);
  206. mutex_init(&data->lock);
  207. /* AD7418 has a curious behaviour on registers 6 and 7. They
  208. * both always read 0xC071 and are not documented on the datasheet.
  209. * We use them to detect the chip.
  210. */
  211. if (kind <= 0) {
  212. int reg, reg6, reg7;
  213. /* the AD7416 lies within this address range, but I have
  214. * no means to check.
  215. */
  216. if (address >= 0x48 && address <= 0x4f) {
  217. /* XXX add tests for AD7416 here */
  218. /* data->type = ad7416; */
  219. }
  220. /* here we might have AD7417 or AD7418 */
  221. else if (address >= 0x28 && address <= 0x2f) {
  222. reg6 = i2c_smbus_read_word_data(client, 0x06);
  223. reg7 = i2c_smbus_read_word_data(client, 0x07);
  224. if (address == 0x28 && reg6 == 0xC071 && reg7 == 0xC071)
  225. data->type = ad7418;
  226. /* XXX add tests for AD7417 here */
  227. /* both AD7417 and AD7418 have bits 0-5 of
  228. * the CONF2 register at 0
  229. */
  230. reg = i2c_smbus_read_byte_data(client,
  231. AD7418_REG_CONF2);
  232. if (reg & 0x3F)
  233. data->type = any_chip; /* detection failed */
  234. }
  235. } else {
  236. dev_dbg(&adapter->dev, "detection forced\n");
  237. }
  238. if (kind > 0)
  239. data->type = kind;
  240. else if (kind < 0 && data->type == any_chip) {
  241. err = -ENODEV;
  242. goto exit_free;
  243. }
  244. switch (data->type) {
  245. case any_chip:
  246. case ad7416:
  247. data->adc_max = 0;
  248. data->attrs.attrs = ad7416_attributes;
  249. strlcpy(client->name, "ad7416", I2C_NAME_SIZE);
  250. break;
  251. case ad7417:
  252. data->adc_max = 4;
  253. data->attrs.attrs = ad7417_attributes;
  254. strlcpy(client->name, "ad7417", I2C_NAME_SIZE);
  255. break;
  256. case ad7418:
  257. data->adc_max = 1;
  258. data->attrs.attrs = ad7418_attributes;
  259. strlcpy(client->name, "ad7418", I2C_NAME_SIZE);
  260. break;
  261. }
  262. if ((err = i2c_attach_client(client)))
  263. goto exit_free;
  264. dev_info(&client->dev, "%s chip found\n", client->name);
  265. /* Initialize the AD7418 chip */
  266. ad7418_init_client(client);
  267. /* Register sysfs hooks */
  268. if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
  269. goto exit_detach;
  270. data->class_dev = hwmon_device_register(&client->dev);
  271. if (IS_ERR(data->class_dev)) {
  272. err = PTR_ERR(data->class_dev);
  273. goto exit_remove;
  274. }
  275. return 0;
  276. exit_remove:
  277. sysfs_remove_group(&client->dev.kobj, &data->attrs);
  278. exit_detach:
  279. i2c_detach_client(client);
  280. exit_free:
  281. kfree(data);
  282. exit:
  283. return err;
  284. }
  285. static int ad7418_detach_client(struct i2c_client *client)
  286. {
  287. struct ad7418_data *data = i2c_get_clientdata(client);
  288. hwmon_device_unregister(data->class_dev);
  289. sysfs_remove_group(&client->dev.kobj, &data->attrs);
  290. i2c_detach_client(client);
  291. kfree(data);
  292. return 0;
  293. }
  294. static int __init ad7418_init(void)
  295. {
  296. return i2c_add_driver(&ad7418_driver);
  297. }
  298. static void __exit ad7418_exit(void)
  299. {
  300. i2c_del_driver(&ad7418_driver);
  301. }
  302. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  303. MODULE_DESCRIPTION("AD7416/17/18 driver");
  304. MODULE_LICENSE("GPL");
  305. MODULE_VERSION(DRV_VERSION);
  306. module_init(ad7418_init);
  307. module_exit(ad7418_exit);