ad7414.c 6.3 KB

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