ad7418.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 i2c_client *client;
  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 struct ad7418_data *ad7418_update_device(struct device *dev)
  49. {
  50. struct ad7418_data *data = dev_get_drvdata(dev);
  51. struct i2c_client *client = data->client;
  52. mutex_lock(&data->lock);
  53. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  54. || !data->valid) {
  55. u8 cfg;
  56. int i, ch;
  57. /* read config register and clear channel bits */
  58. cfg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
  59. cfg &= 0x1F;
  60. i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
  61. cfg | AD7418_CH_TEMP);
  62. udelay(30);
  63. for (i = 0; i < 3; i++) {
  64. data->temp[i] =
  65. i2c_smbus_read_word_swapped(client,
  66. AD7418_REG_TEMP[i]);
  67. }
  68. for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
  69. i2c_smbus_write_byte_data(client,
  70. AD7418_REG_CONF,
  71. cfg | AD7418_REG_ADC_CH(ch));
  72. udelay(15);
  73. data->in[data->adc_max - 1 - i] =
  74. i2c_smbus_read_word_swapped(client,
  75. AD7418_REG_ADC);
  76. }
  77. /* restore old configuration value */
  78. i2c_smbus_write_word_swapped(client, AD7418_REG_CONF, cfg);
  79. data->last_updated = jiffies;
  80. data->valid = 1;
  81. }
  82. mutex_unlock(&data->lock);
  83. return data;
  84. }
  85. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  86. char *buf)
  87. {
  88. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  89. struct ad7418_data *data = ad7418_update_device(dev);
  90. return sprintf(buf, "%d\n",
  91. LM75_TEMP_FROM_REG(data->temp[attr->index]));
  92. }
  93. static ssize_t show_adc(struct device *dev, struct device_attribute *devattr,
  94. char *buf)
  95. {
  96. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  97. struct ad7418_data *data = ad7418_update_device(dev);
  98. return sprintf(buf, "%d\n",
  99. ((data->in[attr->index] >> 6) * 2500 + 512) / 1024);
  100. }
  101. static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
  102. const char *buf, size_t count)
  103. {
  104. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  105. struct ad7418_data *data = dev_get_drvdata(dev);
  106. struct i2c_client *client = data->client;
  107. long temp;
  108. int ret = kstrtol(buf, 10, &temp);
  109. if (ret < 0)
  110. return ret;
  111. mutex_lock(&data->lock);
  112. data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
  113. i2c_smbus_write_word_swapped(client,
  114. AD7418_REG_TEMP[attr->index],
  115. data->temp[attr->index]);
  116. mutex_unlock(&data->lock);
  117. return count;
  118. }
  119. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  120. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  121. show_temp, set_temp, 1);
  122. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  123. show_temp, set_temp, 2);
  124. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_adc, NULL, 0);
  125. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_adc, NULL, 1);
  126. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 2);
  127. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 3);
  128. static struct attribute *ad7416_attrs[] = {
  129. &sensor_dev_attr_temp1_max.dev_attr.attr,
  130. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  131. &sensor_dev_attr_temp1_input.dev_attr.attr,
  132. NULL
  133. };
  134. ATTRIBUTE_GROUPS(ad7416);
  135. static struct attribute *ad7417_attrs[] = {
  136. &sensor_dev_attr_temp1_max.dev_attr.attr,
  137. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  138. &sensor_dev_attr_temp1_input.dev_attr.attr,
  139. &sensor_dev_attr_in1_input.dev_attr.attr,
  140. &sensor_dev_attr_in2_input.dev_attr.attr,
  141. &sensor_dev_attr_in3_input.dev_attr.attr,
  142. &sensor_dev_attr_in4_input.dev_attr.attr,
  143. NULL
  144. };
  145. ATTRIBUTE_GROUPS(ad7417);
  146. static struct attribute *ad7418_attrs[] = {
  147. &sensor_dev_attr_temp1_max.dev_attr.attr,
  148. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  149. &sensor_dev_attr_temp1_input.dev_attr.attr,
  150. &sensor_dev_attr_in1_input.dev_attr.attr,
  151. NULL
  152. };
  153. ATTRIBUTE_GROUPS(ad7418);
  154. static void ad7418_init_client(struct i2c_client *client)
  155. {
  156. struct ad7418_data *data = i2c_get_clientdata(client);
  157. int reg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
  158. if (reg < 0) {
  159. dev_err(&client->dev, "cannot read configuration register\n");
  160. } else {
  161. dev_info(&client->dev, "configuring for mode 1\n");
  162. i2c_smbus_write_byte_data(client, AD7418_REG_CONF, reg & 0xfe);
  163. if (data->type == ad7417 || data->type == ad7418)
  164. i2c_smbus_write_byte_data(client,
  165. AD7418_REG_CONF2, 0x00);
  166. }
  167. }
  168. static int ad7418_probe(struct i2c_client *client,
  169. const struct i2c_device_id *id)
  170. {
  171. struct device *dev = &client->dev;
  172. struct i2c_adapter *adapter = client->adapter;
  173. struct ad7418_data *data;
  174. struct device *hwmon_dev;
  175. const struct attribute_group **attr_groups = NULL;
  176. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  177. I2C_FUNC_SMBUS_WORD_DATA))
  178. return -EOPNOTSUPP;
  179. data = devm_kzalloc(dev, sizeof(struct ad7418_data), GFP_KERNEL);
  180. if (!data)
  181. return -ENOMEM;
  182. i2c_set_clientdata(client, data);
  183. mutex_init(&data->lock);
  184. data->client = client;
  185. data->type = id->driver_data;
  186. switch (data->type) {
  187. case ad7416:
  188. data->adc_max = 0;
  189. attr_groups = ad7416_groups;
  190. break;
  191. case ad7417:
  192. data->adc_max = 4;
  193. attr_groups = ad7417_groups;
  194. break;
  195. case ad7418:
  196. data->adc_max = 1;
  197. attr_groups = ad7418_groups;
  198. break;
  199. }
  200. dev_info(dev, "%s chip found\n", client->name);
  201. /* Initialize the AD7418 chip */
  202. ad7418_init_client(client);
  203. hwmon_dev = devm_hwmon_device_register_with_groups(dev,
  204. client->name,
  205. data, attr_groups);
  206. return PTR_ERR_OR_ZERO(hwmon_dev);
  207. }
  208. static const struct i2c_device_id ad7418_id[] = {
  209. { "ad7416", ad7416 },
  210. { "ad7417", ad7417 },
  211. { "ad7418", ad7418 },
  212. { }
  213. };
  214. MODULE_DEVICE_TABLE(i2c, ad7418_id);
  215. static struct i2c_driver ad7418_driver = {
  216. .driver = {
  217. .name = "ad7418",
  218. },
  219. .probe = ad7418_probe,
  220. .id_table = ad7418_id,
  221. };
  222. module_i2c_driver(ad7418_driver);
  223. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  224. MODULE_DESCRIPTION("AD7416/17/18 driver");
  225. MODULE_LICENSE("GPL");
  226. MODULE_VERSION(DRV_VERSION);