ad7414.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /* AD7414 registers */
  30. #define AD7414_REG_TEMP 0x00
  31. #define AD7414_REG_CONF 0x01
  32. #define AD7414_REG_T_HIGH 0x02
  33. #define AD7414_REG_T_LOW 0x03
  34. static u8 AD7414_REG_LIMIT[] = { AD7414_REG_T_HIGH, AD7414_REG_T_LOW };
  35. struct ad7414_data {
  36. struct device *hwmon_dev;
  37. struct mutex lock; /* atomic read data updates */
  38. char valid; /* !=0 if following fields are valid */
  39. unsigned long next_update; /* In jiffies */
  40. s16 temp_input; /* Register values */
  41. s8 temps[ARRAY_SIZE(AD7414_REG_LIMIT)];
  42. };
  43. /* REG: (0.25C/bit, two's complement) << 6 */
  44. static inline int ad7414_temp_from_reg(s16 reg)
  45. {
  46. /* use integer division instead of equivalent right shift to
  47. * guarantee arithmetic shift and preserve the sign
  48. */
  49. return ((int)reg / 64) * 250;
  50. }
  51. static inline int ad7414_read(struct i2c_client *client, u8 reg)
  52. {
  53. if (reg == AD7414_REG_TEMP) {
  54. int value = i2c_smbus_read_word_data(client, reg);
  55. return (value < 0) ? value : swab16(value);
  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 = 0;
  153. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  154. I2C_FUNC_SMBUS_READ_WORD_DATA))
  155. goto exit;
  156. data = kzalloc(sizeof(struct ad7414_data), GFP_KERNEL);
  157. if (!data) {
  158. err = -ENOMEM;
  159. goto exit;
  160. }
  161. i2c_set_clientdata(client, data);
  162. mutex_init(&data->lock);
  163. dev_info(&client->dev, "chip found\n");
  164. /* Make sure the chip is powered up. */
  165. conf = i2c_smbus_read_byte_data(client, AD7414_REG_CONF);
  166. if (conf < 0)
  167. dev_warn(&client->dev,
  168. "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. /* Register sysfs hooks */
  174. err = sysfs_create_group(&client->dev.kobj, &ad7414_group);
  175. if (err)
  176. goto exit_free;
  177. data->hwmon_dev = hwmon_device_register(&client->dev);
  178. if (IS_ERR(data->hwmon_dev)) {
  179. err = PTR_ERR(data->hwmon_dev);
  180. goto exit_remove;
  181. }
  182. return 0;
  183. exit_remove:
  184. sysfs_remove_group(&client->dev.kobj, &ad7414_group);
  185. exit_free:
  186. kfree(data);
  187. exit:
  188. return err;
  189. }
  190. static int __devexit 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. kfree(data);
  196. return 0;
  197. }
  198. static const struct i2c_device_id ad7414_id[] = {
  199. { "ad7414", 0 },
  200. {}
  201. };
  202. static struct i2c_driver ad7414_driver = {
  203. .driver = {
  204. .name = "ad7414",
  205. },
  206. .probe = ad7414_probe,
  207. .remove = __devexit_p(ad7414_remove),
  208. .id_table = ad7414_id,
  209. };
  210. static int __init ad7414_init(void)
  211. {
  212. return i2c_add_driver(&ad7414_driver);
  213. }
  214. module_init(ad7414_init);
  215. static void __exit ad7414_exit(void)
  216. {
  217. i2c_del_driver(&ad7414_driver);
  218. }
  219. module_exit(ad7414_exit);
  220. MODULE_AUTHOR("Stefan Roese <sr at denx.de>, "
  221. "Frank Edelhaeuser <frank.edelhaeuser at spansion.com>");
  222. MODULE_DESCRIPTION("AD7414 driver");
  223. MODULE_LICENSE("GPL");