ad7414.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. /* use integer division instead of equivalent right shift to
  48. * guarantee arithmetic shift and preserve the sign
  49. */
  50. return ((int)reg / 64) * 250;
  51. }
  52. static inline int ad7414_read(struct i2c_client *client, u8 reg)
  53. {
  54. if (reg == AD7414_REG_TEMP)
  55. return i2c_smbus_read_word_swapped(client, reg);
  56. else
  57. return i2c_smbus_read_byte_data(client, reg);
  58. }
  59. static inline int ad7414_write(struct i2c_client *client, u8 reg, u8 value)
  60. {
  61. return i2c_smbus_write_byte_data(client, reg, value);
  62. }
  63. static struct ad7414_data *ad7414_update_device(struct device *dev)
  64. {
  65. struct i2c_client *client = to_i2c_client(dev);
  66. struct ad7414_data *data = i2c_get_clientdata(client);
  67. mutex_lock(&data->lock);
  68. if (time_after(jiffies, data->next_update) || !data->valid) {
  69. int value, i;
  70. dev_dbg(&client->dev, "starting ad7414 update\n");
  71. value = ad7414_read(client, AD7414_REG_TEMP);
  72. if (value < 0)
  73. dev_dbg(&client->dev, "AD7414_REG_TEMP err %d\n",
  74. value);
  75. else
  76. data->temp_input = value;
  77. for (i = 0; i < ARRAY_SIZE(AD7414_REG_LIMIT); ++i) {
  78. value = ad7414_read(client, AD7414_REG_LIMIT[i]);
  79. if (value < 0)
  80. dev_dbg(&client->dev, "AD7414 reg %d err %d\n",
  81. AD7414_REG_LIMIT[i], value);
  82. else
  83. data->temps[i] = value;
  84. }
  85. data->next_update = jiffies + HZ + HZ / 2;
  86. data->valid = 1;
  87. }
  88. mutex_unlock(&data->lock);
  89. return data;
  90. }
  91. static ssize_t show_temp_input(struct device *dev,
  92. struct device_attribute *attr, char *buf)
  93. {
  94. struct ad7414_data *data = ad7414_update_device(dev);
  95. return sprintf(buf, "%d\n", ad7414_temp_from_reg(data->temp_input));
  96. }
  97. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0);
  98. static ssize_t show_max_min(struct device *dev, struct device_attribute *attr,
  99. char *buf)
  100. {
  101. int index = to_sensor_dev_attr(attr)->index;
  102. struct ad7414_data *data = ad7414_update_device(dev);
  103. return sprintf(buf, "%d\n", data->temps[index] * 1000);
  104. }
  105. static ssize_t set_max_min(struct device *dev,
  106. struct device_attribute *attr,
  107. const char *buf, size_t count)
  108. {
  109. struct i2c_client *client = to_i2c_client(dev);
  110. struct ad7414_data *data = i2c_get_clientdata(client);
  111. int index = to_sensor_dev_attr(attr)->index;
  112. u8 reg = AD7414_REG_LIMIT[index];
  113. long temp = simple_strtol(buf, NULL, 10);
  114. temp = SENSORS_LIMIT(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(temp1_max, S_IWUSR | S_IRUGO,
  123. show_max_min, set_max_min, 0);
  124. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  125. show_max_min, set_max_min, 1);
  126. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  127. char *buf)
  128. {
  129. int bitnr = to_sensor_dev_attr(attr)->index;
  130. struct ad7414_data *data = ad7414_update_device(dev);
  131. int value = (data->temp_input >> bitnr) & 1;
  132. return sprintf(buf, "%d\n", value);
  133. }
  134. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  135. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  136. static struct attribute *ad7414_attributes[] = {
  137. &sensor_dev_attr_temp1_input.dev_attr.attr,
  138. &sensor_dev_attr_temp1_max.dev_attr.attr,
  139. &sensor_dev_attr_temp1_min.dev_attr.attr,
  140. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  141. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  142. NULL
  143. };
  144. static const struct attribute_group ad7414_group = {
  145. .attrs = ad7414_attributes,
  146. };
  147. static int ad7414_probe(struct i2c_client *client,
  148. const struct i2c_device_id *dev_id)
  149. {
  150. struct ad7414_data *data;
  151. int conf;
  152. int err;
  153. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  154. I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  155. err = -EOPNOTSUPP;
  156. goto exit;
  157. }
  158. data = kzalloc(sizeof(struct ad7414_data), GFP_KERNEL);
  159. if (!data) {
  160. err = -ENOMEM;
  161. goto exit;
  162. }
  163. i2c_set_clientdata(client, data);
  164. mutex_init(&data->lock);
  165. dev_info(&client->dev, "chip found\n");
  166. /* Make sure the chip is powered up. */
  167. conf = i2c_smbus_read_byte_data(client, AD7414_REG_CONF);
  168. if (conf < 0)
  169. dev_warn(&client->dev,
  170. "ad7414_probe unable to read config register.\n");
  171. else {
  172. conf &= ~(1 << 7);
  173. i2c_smbus_write_byte_data(client, AD7414_REG_CONF, conf);
  174. }
  175. /* Register sysfs hooks */
  176. err = sysfs_create_group(&client->dev.kobj, &ad7414_group);
  177. if (err)
  178. goto exit_free;
  179. data->hwmon_dev = hwmon_device_register(&client->dev);
  180. if (IS_ERR(data->hwmon_dev)) {
  181. err = PTR_ERR(data->hwmon_dev);
  182. goto exit_remove;
  183. }
  184. return 0;
  185. exit_remove:
  186. sysfs_remove_group(&client->dev.kobj, &ad7414_group);
  187. exit_free:
  188. kfree(data);
  189. exit:
  190. return err;
  191. }
  192. static int __devexit ad7414_remove(struct i2c_client *client)
  193. {
  194. struct ad7414_data *data = i2c_get_clientdata(client);
  195. hwmon_device_unregister(data->hwmon_dev);
  196. sysfs_remove_group(&client->dev.kobj, &ad7414_group);
  197. kfree(data);
  198. return 0;
  199. }
  200. static const struct i2c_device_id ad7414_id[] = {
  201. { "ad7414", 0 },
  202. {}
  203. };
  204. MODULE_DEVICE_TABLE(i2c, ad7414_id);
  205. static struct i2c_driver ad7414_driver = {
  206. .driver = {
  207. .name = "ad7414",
  208. },
  209. .probe = ad7414_probe,
  210. .remove = __devexit_p(ad7414_remove),
  211. .id_table = ad7414_id,
  212. };
  213. static int __init ad7414_init(void)
  214. {
  215. return i2c_add_driver(&ad7414_driver);
  216. }
  217. module_init(ad7414_init);
  218. static void __exit ad7414_exit(void)
  219. {
  220. i2c_del_driver(&ad7414_driver);
  221. }
  222. module_exit(ad7414_exit);
  223. MODULE_AUTHOR("Stefan Roese <sr at denx.de>, "
  224. "Frank Edelhaeuser <frank.edelhaeuser at spansion.com>");
  225. MODULE_DESCRIPTION("AD7414 driver");
  226. MODULE_LICENSE("GPL");