ad7418.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * An hwmon driver for the Analog Devices AD7416/17/18
  4. * Copyright (C) 2006-07 Tower Technologies
  5. *
  6. * Author: Alessandro Zummo <a.zummo@towertech.it>
  7. *
  8. * Based on lm75.c
  9. * Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/i2c.h>
  14. #include <linux/hwmon.h>
  15. #include <linux/hwmon-sysfs.h>
  16. #include <linux/err.h>
  17. #include <linux/mutex.h>
  18. #include <linux/of.h>
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include "lm75.h"
  22. #define DRV_VERSION "0.4"
  23. enum chips { ad7416, ad7417, ad7418 };
  24. /* AD7418 registers */
  25. #define AD7418_REG_TEMP_IN 0x00
  26. #define AD7418_REG_CONF 0x01
  27. #define AD7418_REG_TEMP_HYST 0x02
  28. #define AD7418_REG_TEMP_OS 0x03
  29. #define AD7418_REG_ADC 0x04
  30. #define AD7418_REG_CONF2 0x05
  31. #define AD7418_REG_ADC_CH(x) ((x) << 5)
  32. #define AD7418_CH_TEMP AD7418_REG_ADC_CH(0)
  33. static const u8 AD7418_REG_TEMP[] = { AD7418_REG_TEMP_IN,
  34. AD7418_REG_TEMP_HYST,
  35. AD7418_REG_TEMP_OS };
  36. struct ad7418_data {
  37. struct i2c_client *client;
  38. enum chips type;
  39. struct mutex lock;
  40. int adc_max; /* number of ADC channels */
  41. bool valid;
  42. unsigned long last_updated; /* In jiffies */
  43. s16 temp[3]; /* Register values */
  44. u16 in[4];
  45. };
  46. static int ad7418_update_device(struct device *dev)
  47. {
  48. struct ad7418_data *data = dev_get_drvdata(dev);
  49. struct i2c_client *client = data->client;
  50. s32 val;
  51. mutex_lock(&data->lock);
  52. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  53. || !data->valid) {
  54. u8 cfg;
  55. int i, ch;
  56. /* read config register and clear channel bits */
  57. val = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
  58. if (val < 0)
  59. goto abort;
  60. cfg = val;
  61. cfg &= 0x1F;
  62. val = i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
  63. cfg | AD7418_CH_TEMP);
  64. if (val < 0)
  65. goto abort;
  66. udelay(30);
  67. for (i = 0; i < 3; i++) {
  68. val = i2c_smbus_read_word_swapped(client,
  69. AD7418_REG_TEMP[i]);
  70. if (val < 0)
  71. goto abort;
  72. data->temp[i] = val;
  73. }
  74. for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
  75. val = i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
  76. cfg | AD7418_REG_ADC_CH(ch));
  77. if (val < 0)
  78. goto abort;
  79. udelay(15);
  80. val = i2c_smbus_read_word_swapped(client,
  81. AD7418_REG_ADC);
  82. if (val < 0)
  83. goto abort;
  84. data->in[data->adc_max - 1 - i] = val;
  85. }
  86. /* restore old configuration value */
  87. val = i2c_smbus_write_word_swapped(client, AD7418_REG_CONF,
  88. cfg);
  89. if (val < 0)
  90. goto abort;
  91. data->last_updated = jiffies;
  92. data->valid = true;
  93. }
  94. mutex_unlock(&data->lock);
  95. return 0;
  96. abort:
  97. data->valid = false;
  98. mutex_unlock(&data->lock);
  99. return val;
  100. }
  101. static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
  102. char *buf)
  103. {
  104. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  105. struct ad7418_data *data = dev_get_drvdata(dev);
  106. int ret;
  107. ret = ad7418_update_device(dev);
  108. if (ret < 0)
  109. return ret;
  110. return sprintf(buf, "%d\n",
  111. LM75_TEMP_FROM_REG(data->temp[attr->index]));
  112. }
  113. static ssize_t adc_show(struct device *dev, struct device_attribute *devattr,
  114. char *buf)
  115. {
  116. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  117. struct ad7418_data *data = dev_get_drvdata(dev);
  118. int ret;
  119. ret = ad7418_update_device(dev);
  120. if (ret < 0)
  121. return ret;
  122. return sprintf(buf, "%d\n",
  123. ((data->in[attr->index] >> 6) * 2500 + 512) / 1024);
  124. }
  125. static ssize_t temp_store(struct device *dev,
  126. struct device_attribute *devattr, const char *buf,
  127. size_t count)
  128. {
  129. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  130. struct ad7418_data *data = dev_get_drvdata(dev);
  131. struct i2c_client *client = data->client;
  132. long temp;
  133. int ret = kstrtol(buf, 10, &temp);
  134. if (ret < 0)
  135. return ret;
  136. mutex_lock(&data->lock);
  137. data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
  138. i2c_smbus_write_word_swapped(client,
  139. AD7418_REG_TEMP[attr->index],
  140. data->temp[attr->index]);
  141. mutex_unlock(&data->lock);
  142. return count;
  143. }
  144. static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
  145. static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp, 1);
  146. static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 2);
  147. static SENSOR_DEVICE_ATTR_RO(in1_input, adc, 0);
  148. static SENSOR_DEVICE_ATTR_RO(in2_input, adc, 1);
  149. static SENSOR_DEVICE_ATTR_RO(in3_input, adc, 2);
  150. static SENSOR_DEVICE_ATTR_RO(in4_input, adc, 3);
  151. static struct attribute *ad7416_attrs[] = {
  152. &sensor_dev_attr_temp1_max.dev_attr.attr,
  153. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  154. &sensor_dev_attr_temp1_input.dev_attr.attr,
  155. NULL
  156. };
  157. ATTRIBUTE_GROUPS(ad7416);
  158. static struct attribute *ad7417_attrs[] = {
  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. &sensor_dev_attr_in1_input.dev_attr.attr,
  163. &sensor_dev_attr_in2_input.dev_attr.attr,
  164. &sensor_dev_attr_in3_input.dev_attr.attr,
  165. &sensor_dev_attr_in4_input.dev_attr.attr,
  166. NULL
  167. };
  168. ATTRIBUTE_GROUPS(ad7417);
  169. static struct attribute *ad7418_attrs[] = {
  170. &sensor_dev_attr_temp1_max.dev_attr.attr,
  171. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  172. &sensor_dev_attr_temp1_input.dev_attr.attr,
  173. &sensor_dev_attr_in1_input.dev_attr.attr,
  174. NULL
  175. };
  176. ATTRIBUTE_GROUPS(ad7418);
  177. static void ad7418_init_client(struct i2c_client *client)
  178. {
  179. struct ad7418_data *data = i2c_get_clientdata(client);
  180. int reg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
  181. if (reg < 0) {
  182. dev_err(&client->dev, "cannot read configuration register\n");
  183. } else {
  184. dev_info(&client->dev, "configuring for mode 1\n");
  185. i2c_smbus_write_byte_data(client, AD7418_REG_CONF, reg & 0xfe);
  186. if (data->type == ad7417 || data->type == ad7418)
  187. i2c_smbus_write_byte_data(client,
  188. AD7418_REG_CONF2, 0x00);
  189. }
  190. }
  191. static int ad7418_probe(struct i2c_client *client)
  192. {
  193. struct device *dev = &client->dev;
  194. struct i2c_adapter *adapter = client->adapter;
  195. struct ad7418_data *data;
  196. struct device *hwmon_dev;
  197. const struct attribute_group **attr_groups = NULL;
  198. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  199. I2C_FUNC_SMBUS_WORD_DATA))
  200. return -EOPNOTSUPP;
  201. data = devm_kzalloc(dev, sizeof(struct ad7418_data), GFP_KERNEL);
  202. if (!data)
  203. return -ENOMEM;
  204. i2c_set_clientdata(client, data);
  205. mutex_init(&data->lock);
  206. data->client = client;
  207. data->type = (uintptr_t)i2c_get_match_data(client);
  208. switch (data->type) {
  209. case ad7416:
  210. data->adc_max = 0;
  211. attr_groups = ad7416_groups;
  212. break;
  213. case ad7417:
  214. data->adc_max = 4;
  215. attr_groups = ad7417_groups;
  216. break;
  217. case ad7418:
  218. data->adc_max = 1;
  219. attr_groups = ad7418_groups;
  220. break;
  221. }
  222. dev_info(dev, "%s chip found\n", client->name);
  223. /* Initialize the AD7418 chip */
  224. ad7418_init_client(client);
  225. hwmon_dev = devm_hwmon_device_register_with_groups(dev,
  226. client->name,
  227. data, attr_groups);
  228. return PTR_ERR_OR_ZERO(hwmon_dev);
  229. }
  230. static const struct i2c_device_id ad7418_id[] = {
  231. { "ad7416", ad7416 },
  232. { "ad7417", ad7417 },
  233. { "ad7418", ad7418 },
  234. { }
  235. };
  236. MODULE_DEVICE_TABLE(i2c, ad7418_id);
  237. static const struct of_device_id ad7418_dt_ids[] = {
  238. { .compatible = "adi,ad7416", .data = (void *)ad7416, },
  239. { .compatible = "adi,ad7417", .data = (void *)ad7417, },
  240. { .compatible = "adi,ad7418", .data = (void *)ad7418, },
  241. { }
  242. };
  243. MODULE_DEVICE_TABLE(of, ad7418_dt_ids);
  244. static struct i2c_driver ad7418_driver = {
  245. .driver = {
  246. .name = "ad7418",
  247. .of_match_table = ad7418_dt_ids,
  248. },
  249. .probe = ad7418_probe,
  250. .id_table = ad7418_id,
  251. };
  252. module_i2c_driver(ad7418_driver);
  253. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  254. MODULE_DESCRIPTION("AD7416/17/18 driver");
  255. MODULE_LICENSE("GPL");
  256. MODULE_VERSION(DRV_VERSION);