ad7414.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. int value = i2c_smbus_read_word_data(client, reg);
  56. return (value < 0) ? value : swab16(value);
  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 = simple_strtol(buf, NULL, 10);
  115. temp = SENSORS_LIMIT(temp, -40000, 85000);
  116. temp = (temp + (temp < 0 ? -500 : 500)) / 1000;
  117. mutex_lock(&data->lock);
  118. data->temps[index] = temp;
  119. ad7414_write(client, reg, temp);
  120. mutex_unlock(&data->lock);
  121. return count;
  122. }
  123. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  124. show_max_min, set_max_min, 0);
  125. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  126. show_max_min, set_max_min, 1);
  127. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  128. char *buf)
  129. {
  130. int bitnr = to_sensor_dev_attr(attr)->index;
  131. struct ad7414_data *data = ad7414_update_device(dev);
  132. int value = (data->temp_input >> bitnr) & 1;
  133. return sprintf(buf, "%d\n", value);
  134. }
  135. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  136. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  137. static struct attribute *ad7414_attributes[] = {
  138. &sensor_dev_attr_temp1_input.dev_attr.attr,
  139. &sensor_dev_attr_temp1_max.dev_attr.attr,
  140. &sensor_dev_attr_temp1_min.dev_attr.attr,
  141. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  142. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  143. NULL
  144. };
  145. static const struct attribute_group ad7414_group = {
  146. .attrs = ad7414_attributes,
  147. };
  148. static int ad7414_probe(struct i2c_client *client,
  149. const struct i2c_device_id *dev_id)
  150. {
  151. struct ad7414_data *data;
  152. int conf;
  153. int err;
  154. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  155. I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  156. err = -EOPNOTSUPP;
  157. goto exit;
  158. }
  159. data = kzalloc(sizeof(struct ad7414_data), GFP_KERNEL);
  160. if (!data) {
  161. err = -ENOMEM;
  162. goto exit;
  163. }
  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. goto exit_free;
  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. exit_free:
  189. kfree(data);
  190. exit:
  191. return err;
  192. }
  193. static int __devexit ad7414_remove(struct i2c_client *client)
  194. {
  195. struct ad7414_data *data = i2c_get_clientdata(client);
  196. hwmon_device_unregister(data->hwmon_dev);
  197. sysfs_remove_group(&client->dev.kobj, &ad7414_group);
  198. kfree(data);
  199. return 0;
  200. }
  201. static const struct i2c_device_id ad7414_id[] = {
  202. { "ad7414", 0 },
  203. {}
  204. };
  205. MODULE_DEVICE_TABLE(i2c, ad7414_id);
  206. static struct i2c_driver ad7414_driver = {
  207. .driver = {
  208. .name = "ad7414",
  209. },
  210. .probe = ad7414_probe,
  211. .remove = __devexit_p(ad7414_remove),
  212. .id_table = ad7414_id,
  213. };
  214. static int __init ad7414_init(void)
  215. {
  216. return i2c_add_driver(&ad7414_driver);
  217. }
  218. module_init(ad7414_init);
  219. static void __exit ad7414_exit(void)
  220. {
  221. i2c_del_driver(&ad7414_driver);
  222. }
  223. module_exit(ad7414_exit);
  224. MODULE_AUTHOR("Stefan Roese <sr at denx.de>, "
  225. "Frank Edelhaeuser <frank.edelhaeuser at spansion.com>");
  226. MODULE_DESCRIPTION("AD7414 driver");
  227. MODULE_LICENSE("GPL");