ad7414.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 device *hwmon_dev;
  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 i2c_client *client = to_i2c_client(dev);
  67. struct ad7414_data *data = i2c_get_clientdata(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 i2c_client *client = to_i2c_client(dev);
  111. struct ad7414_data *data = i2c_get_clientdata(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_attributes[] = {
  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. static const struct attribute_group ad7414_group = {
  149. .attrs = ad7414_attributes,
  150. };
  151. static int ad7414_probe(struct i2c_client *client,
  152. const struct i2c_device_id *dev_id)
  153. {
  154. struct ad7414_data *data;
  155. int conf;
  156. int err;
  157. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  158. I2C_FUNC_SMBUS_READ_WORD_DATA))
  159. return -EOPNOTSUPP;
  160. data = devm_kzalloc(&client->dev, sizeof(struct ad7414_data),
  161. GFP_KERNEL);
  162. if (!data)
  163. return -ENOMEM;
  164. i2c_set_clientdata(client, data);
  165. mutex_init(&data->lock);
  166. dev_info(&client->dev, "chip found\n");
  167. /* Make sure the chip is powered up. */
  168. conf = i2c_smbus_read_byte_data(client, AD7414_REG_CONF);
  169. if (conf < 0)
  170. dev_warn(&client->dev,
  171. "ad7414_probe unable to read config register.\n");
  172. else {
  173. conf &= ~(1 << 7);
  174. i2c_smbus_write_byte_data(client, AD7414_REG_CONF, conf);
  175. }
  176. /* Register sysfs hooks */
  177. err = sysfs_create_group(&client->dev.kobj, &ad7414_group);
  178. if (err)
  179. return err;
  180. data->hwmon_dev = hwmon_device_register(&client->dev);
  181. if (IS_ERR(data->hwmon_dev)) {
  182. err = PTR_ERR(data->hwmon_dev);
  183. goto exit_remove;
  184. }
  185. return 0;
  186. exit_remove:
  187. sysfs_remove_group(&client->dev.kobj, &ad7414_group);
  188. return err;
  189. }
  190. static int ad7414_remove(struct i2c_client *client)
  191. {
  192. struct ad7414_data *data = i2c_get_clientdata(client);
  193. hwmon_device_unregister(data->hwmon_dev);
  194. sysfs_remove_group(&client->dev.kobj, &ad7414_group);
  195. return 0;
  196. }
  197. static const struct i2c_device_id ad7414_id[] = {
  198. { "ad7414", 0 },
  199. {}
  200. };
  201. MODULE_DEVICE_TABLE(i2c, ad7414_id);
  202. static struct i2c_driver ad7414_driver = {
  203. .driver = {
  204. .name = "ad7414",
  205. },
  206. .probe = ad7414_probe,
  207. .remove = ad7414_remove,
  208. .id_table = ad7414_id,
  209. };
  210. module_i2c_driver(ad7414_driver);
  211. MODULE_AUTHOR("Stefan Roese <sr at denx.de>, "
  212. "Frank Edelhaeuser <frank.edelhaeuser at spansion.com>");
  213. MODULE_DESCRIPTION("AD7414 driver");
  214. MODULE_LICENSE("GPL");