ad7414.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * An hwmon driver for the Analog Devices AD7414
  4. *
  5. * Copyright 2006 Stefan Roese <sr at denx.de>, DENX Software Engineering
  6. *
  7. * Copyright (c) 2008 PIKA Technologies
  8. * Sean MacLennan <[email protected]>
  9. *
  10. * Copyright (c) 2008 Spansion Inc.
  11. * Frank Edelhaeuser <frank.edelhaeuser at spansion.com>
  12. * (converted to "new style" I2C driver model, removed checkpatch.pl warnings)
  13. *
  14. * Based on ad7418.c
  15. * Copyright 2006 Tower Technologies, Alessandro Zummo <a.zummo at towertech.it>
  16. */
  17. #include <linux/module.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/i2c.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. #include <linux/err.h>
  23. #include <linux/mutex.h>
  24. #include <linux/sysfs.h>
  25. #include <linux/slab.h>
  26. /* AD7414 registers */
  27. #define AD7414_REG_TEMP 0x00
  28. #define AD7414_REG_CONF 0x01
  29. #define AD7414_REG_T_HIGH 0x02
  30. #define AD7414_REG_T_LOW 0x03
  31. static u8 AD7414_REG_LIMIT[] = { AD7414_REG_T_HIGH, AD7414_REG_T_LOW };
  32. struct ad7414_data {
  33. struct i2c_client *client;
  34. struct mutex lock; /* atomic read data updates */
  35. bool valid; /* true if following fields are valid */
  36. unsigned long next_update; /* In jiffies */
  37. s16 temp_input; /* Register values */
  38. s8 temps[ARRAY_SIZE(AD7414_REG_LIMIT)];
  39. };
  40. /* REG: (0.25C/bit, two's complement) << 6 */
  41. static inline int ad7414_temp_from_reg(s16 reg)
  42. {
  43. /*
  44. * use integer division instead of equivalent right shift to
  45. * guarantee arithmetic shift and preserve the sign
  46. */
  47. return ((int)reg / 64) * 250;
  48. }
  49. static inline int ad7414_read(struct i2c_client *client, u8 reg)
  50. {
  51. if (reg == AD7414_REG_TEMP)
  52. return i2c_smbus_read_word_swapped(client, reg);
  53. else
  54. return i2c_smbus_read_byte_data(client, reg);
  55. }
  56. static inline int ad7414_write(struct i2c_client *client, u8 reg, u8 value)
  57. {
  58. return i2c_smbus_write_byte_data(client, reg, value);
  59. }
  60. static struct ad7414_data *ad7414_update_device(struct device *dev)
  61. {
  62. struct ad7414_data *data = dev_get_drvdata(dev);
  63. struct i2c_client *client = data->client;
  64. mutex_lock(&data->lock);
  65. if (time_after(jiffies, data->next_update) || !data->valid) {
  66. int value, i;
  67. dev_dbg(&client->dev, "starting ad7414 update\n");
  68. value = ad7414_read(client, AD7414_REG_TEMP);
  69. if (value < 0)
  70. dev_dbg(&client->dev, "AD7414_REG_TEMP err %d\n",
  71. value);
  72. else
  73. data->temp_input = value;
  74. for (i = 0; i < ARRAY_SIZE(AD7414_REG_LIMIT); ++i) {
  75. value = ad7414_read(client, AD7414_REG_LIMIT[i]);
  76. if (value < 0)
  77. dev_dbg(&client->dev, "AD7414 reg %d err %d\n",
  78. AD7414_REG_LIMIT[i], value);
  79. else
  80. data->temps[i] = value;
  81. }
  82. data->next_update = jiffies + HZ + HZ / 2;
  83. data->valid = true;
  84. }
  85. mutex_unlock(&data->lock);
  86. return data;
  87. }
  88. static ssize_t temp_input_show(struct device *dev,
  89. struct device_attribute *attr, char *buf)
  90. {
  91. struct ad7414_data *data = ad7414_update_device(dev);
  92. return sprintf(buf, "%d\n", ad7414_temp_from_reg(data->temp_input));
  93. }
  94. static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
  95. static ssize_t max_min_show(struct device *dev, struct device_attribute *attr,
  96. char *buf)
  97. {
  98. int index = to_sensor_dev_attr(attr)->index;
  99. struct ad7414_data *data = ad7414_update_device(dev);
  100. return sprintf(buf, "%d\n", data->temps[index] * 1000);
  101. }
  102. static ssize_t max_min_store(struct device *dev,
  103. struct device_attribute *attr, const char *buf,
  104. size_t count)
  105. {
  106. struct ad7414_data *data = dev_get_drvdata(dev);
  107. struct i2c_client *client = data->client;
  108. int index = to_sensor_dev_attr(attr)->index;
  109. u8 reg = AD7414_REG_LIMIT[index];
  110. long temp;
  111. int ret = kstrtol(buf, 10, &temp);
  112. if (ret < 0)
  113. return ret;
  114. temp = clamp_val(temp, -40000, 85000);
  115. temp = (temp + (temp < 0 ? -500 : 500)) / 1000;
  116. mutex_lock(&data->lock);
  117. data->temps[index] = temp;
  118. ad7414_write(client, reg, temp);
  119. mutex_unlock(&data->lock);
  120. return count;
  121. }
  122. static SENSOR_DEVICE_ATTR_RW(temp1_max, max_min, 0);
  123. static SENSOR_DEVICE_ATTR_RW(temp1_min, max_min, 1);
  124. static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
  125. char *buf)
  126. {
  127. int bitnr = to_sensor_dev_attr(attr)->index;
  128. struct ad7414_data *data = ad7414_update_device(dev);
  129. int value = (data->temp_input >> bitnr) & 1;
  130. return sprintf(buf, "%d\n", value);
  131. }
  132. static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 3);
  133. static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 4);
  134. static struct attribute *ad7414_attrs[] = {
  135. &sensor_dev_attr_temp1_input.dev_attr.attr,
  136. &sensor_dev_attr_temp1_max.dev_attr.attr,
  137. &sensor_dev_attr_temp1_min.dev_attr.attr,
  138. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  139. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  140. NULL
  141. };
  142. ATTRIBUTE_GROUPS(ad7414);
  143. static int ad7414_probe(struct i2c_client *client)
  144. {
  145. struct device *dev = &client->dev;
  146. struct ad7414_data *data;
  147. struct device *hwmon_dev;
  148. int conf;
  149. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  150. I2C_FUNC_SMBUS_READ_WORD_DATA))
  151. return -EOPNOTSUPP;
  152. data = devm_kzalloc(dev, sizeof(struct ad7414_data), GFP_KERNEL);
  153. if (!data)
  154. return -ENOMEM;
  155. data->client = client;
  156. mutex_init(&data->lock);
  157. dev_info(&client->dev, "chip found\n");
  158. /* Make sure the chip is powered up. */
  159. conf = i2c_smbus_read_byte_data(client, AD7414_REG_CONF);
  160. if (conf < 0)
  161. dev_warn(dev, "ad7414_probe unable to read config register.\n");
  162. else {
  163. conf &= ~(1 << 7);
  164. i2c_smbus_write_byte_data(client, AD7414_REG_CONF, conf);
  165. }
  166. hwmon_dev = devm_hwmon_device_register_with_groups(dev,
  167. client->name,
  168. data, ad7414_groups);
  169. return PTR_ERR_OR_ZERO(hwmon_dev);
  170. }
  171. static const struct i2c_device_id ad7414_id[] = {
  172. { "ad7414", 0 },
  173. {}
  174. };
  175. MODULE_DEVICE_TABLE(i2c, ad7414_id);
  176. static const struct of_device_id __maybe_unused ad7414_of_match[] = {
  177. { .compatible = "ad,ad7414" },
  178. { },
  179. };
  180. MODULE_DEVICE_TABLE(of, ad7414_of_match);
  181. static struct i2c_driver ad7414_driver = {
  182. .driver = {
  183. .name = "ad7414",
  184. .of_match_table = of_match_ptr(ad7414_of_match),
  185. },
  186. .probe_new = ad7414_probe,
  187. .id_table = ad7414_id,
  188. };
  189. module_i2c_driver(ad7414_driver);
  190. MODULE_AUTHOR("Stefan Roese <sr at denx.de>, "
  191. "Frank Edelhaeuser <frank.edelhaeuser at spansion.com>");
  192. MODULE_DESCRIPTION("AD7414 driver");
  193. MODULE_LICENSE("GPL");