lm73.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * LM73 Sensor driver
  3. * Based on LM75
  4. *
  5. * Copyright (C) 2007, CenoSYS (www.cenosys.com).
  6. * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu).
  7. *
  8. * Guillaume Ligneul <guillaume.ligneul@gmail.com>
  9. * Adrien Demarez <adrien.demarez@bolloretelecom.eu>
  10. * Jeremy Laine <jeremy.laine@bolloretelecom.eu>
  11. * Chris Verges <kg4ysn@gmail.com>
  12. *
  13. * This software program is licensed subject to the GNU General Public License
  14. * (GPL).Version 2,June 1991, available at
  15. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/i2c.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. #include <linux/err.h>
  23. /* Addresses scanned */
  24. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c,
  25. 0x4d, 0x4e, I2C_CLIENT_END };
  26. /* LM73 registers */
  27. #define LM73_REG_INPUT 0x00
  28. #define LM73_REG_CONF 0x01
  29. #define LM73_REG_MAX 0x02
  30. #define LM73_REG_MIN 0x03
  31. #define LM73_REG_CTRL 0x04
  32. #define LM73_REG_ID 0x07
  33. #define LM73_ID 0x9001 /* 0x0190, byte-swapped */
  34. #define DRVNAME "lm73"
  35. #define LM73_TEMP_MIN (-256000 / 250)
  36. #define LM73_TEMP_MAX (255750 / 250)
  37. #define LM73_CTRL_RES_SHIFT 5
  38. #define LM73_CTRL_RES_MASK (BIT(5) | BIT(6))
  39. #define LM73_CTRL_TO_MASK BIT(7)
  40. #define LM73_CTRL_HI_SHIFT 2
  41. #define LM73_CTRL_LO_SHIFT 1
  42. static const unsigned short lm73_convrates[] = {
  43. 14, /* 11-bits (0.25000 C/LSB): RES1 Bit = 0, RES0 Bit = 0 */
  44. 28, /* 12-bits (0.12500 C/LSB): RES1 Bit = 0, RES0 Bit = 1 */
  45. 56, /* 13-bits (0.06250 C/LSB): RES1 Bit = 1, RES0 Bit = 0 */
  46. 112, /* 14-bits (0.03125 C/LSB): RES1 Bit = 1, RES0 Bit = 1 */
  47. };
  48. struct lm73_data {
  49. struct device *hwmon_dev;
  50. struct mutex lock;
  51. u8 ctrl; /* control register value */
  52. };
  53. /*-----------------------------------------------------------------------*/
  54. static ssize_t set_temp(struct device *dev, struct device_attribute *da,
  55. const char *buf, size_t count)
  56. {
  57. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  58. struct i2c_client *client = to_i2c_client(dev);
  59. long temp;
  60. short value;
  61. s32 err;
  62. int status = kstrtol(buf, 10, &temp);
  63. if (status < 0)
  64. return status;
  65. /* Write value */
  66. value = clamp_val(temp / 250, LM73_TEMP_MIN, LM73_TEMP_MAX) << 5;
  67. err = i2c_smbus_write_word_swapped(client, attr->index, value);
  68. return (err < 0) ? err : count;
  69. }
  70. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  71. char *buf)
  72. {
  73. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  74. struct i2c_client *client = to_i2c_client(dev);
  75. int temp;
  76. s32 err = i2c_smbus_read_word_swapped(client, attr->index);
  77. if (err < 0)
  78. return err;
  79. /* use integer division instead of equivalent right shift to
  80. guarantee arithmetic shift and preserve the sign */
  81. temp = (((s16) err) * 250) / 32;
  82. return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
  83. }
  84. static ssize_t set_convrate(struct device *dev, struct device_attribute *da,
  85. const char *buf, size_t count)
  86. {
  87. struct i2c_client *client = to_i2c_client(dev);
  88. struct lm73_data *data = i2c_get_clientdata(client);
  89. unsigned long convrate;
  90. s32 err;
  91. int res = 0;
  92. err = kstrtoul(buf, 10, &convrate);
  93. if (err < 0)
  94. return err;
  95. /*
  96. * Convert the desired conversion rate into register bits.
  97. * res is already initialized, and everything past the second-to-last
  98. * value in the array is treated as belonging to the last value
  99. * in the array.
  100. */
  101. while (res < (ARRAY_SIZE(lm73_convrates) - 1) &&
  102. convrate > lm73_convrates[res])
  103. res++;
  104. mutex_lock(&data->lock);
  105. data->ctrl &= LM73_CTRL_TO_MASK;
  106. data->ctrl |= res << LM73_CTRL_RES_SHIFT;
  107. err = i2c_smbus_write_byte_data(client, LM73_REG_CTRL, data->ctrl);
  108. mutex_unlock(&data->lock);
  109. if (err < 0)
  110. return err;
  111. return count;
  112. }
  113. static ssize_t show_convrate(struct device *dev, struct device_attribute *da,
  114. char *buf)
  115. {
  116. struct i2c_client *client = to_i2c_client(dev);
  117. struct lm73_data *data = i2c_get_clientdata(client);
  118. int res;
  119. res = (data->ctrl & LM73_CTRL_RES_MASK) >> LM73_CTRL_RES_SHIFT;
  120. return scnprintf(buf, PAGE_SIZE, "%hu\n", lm73_convrates[res]);
  121. }
  122. static ssize_t show_maxmin_alarm(struct device *dev,
  123. struct device_attribute *da, char *buf)
  124. {
  125. struct i2c_client *client = to_i2c_client(dev);
  126. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  127. struct lm73_data *data = i2c_get_clientdata(client);
  128. s32 ctrl;
  129. mutex_lock(&data->lock);
  130. ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL);
  131. if (ctrl < 0)
  132. goto abort;
  133. data->ctrl = ctrl;
  134. mutex_unlock(&data->lock);
  135. return scnprintf(buf, PAGE_SIZE, "%d\n", (ctrl >> attr->index) & 1);
  136. abort:
  137. mutex_unlock(&data->lock);
  138. return ctrl;
  139. }
  140. /*-----------------------------------------------------------------------*/
  141. /* sysfs attributes for hwmon */
  142. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  143. show_temp, set_temp, LM73_REG_MAX);
  144. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  145. show_temp, set_temp, LM73_REG_MIN);
  146. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  147. show_temp, NULL, LM73_REG_INPUT);
  148. static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO,
  149. show_convrate, set_convrate, 0);
  150. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
  151. show_maxmin_alarm, NULL, LM73_CTRL_HI_SHIFT);
  152. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO,
  153. show_maxmin_alarm, NULL, LM73_CTRL_LO_SHIFT);
  154. static struct attribute *lm73_attributes[] = {
  155. &sensor_dev_attr_temp1_input.dev_attr.attr,
  156. &sensor_dev_attr_temp1_max.dev_attr.attr,
  157. &sensor_dev_attr_temp1_min.dev_attr.attr,
  158. &sensor_dev_attr_update_interval.dev_attr.attr,
  159. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  160. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  161. NULL
  162. };
  163. static const struct attribute_group lm73_group = {
  164. .attrs = lm73_attributes,
  165. };
  166. /*-----------------------------------------------------------------------*/
  167. /* device probe and removal */
  168. static int
  169. lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
  170. {
  171. int status;
  172. struct lm73_data *data;
  173. int ctrl;
  174. data = devm_kzalloc(&client->dev, sizeof(struct lm73_data),
  175. GFP_KERNEL);
  176. if (!data)
  177. return -ENOMEM;
  178. i2c_set_clientdata(client, data);
  179. mutex_init(&data->lock);
  180. ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL);
  181. if (ctrl < 0)
  182. return ctrl;
  183. data->ctrl = ctrl;
  184. /* Register sysfs hooks */
  185. status = sysfs_create_group(&client->dev.kobj, &lm73_group);
  186. if (status)
  187. return status;
  188. data->hwmon_dev = hwmon_device_register(&client->dev);
  189. if (IS_ERR(data->hwmon_dev)) {
  190. status = PTR_ERR(data->hwmon_dev);
  191. goto exit_remove;
  192. }
  193. dev_info(&client->dev, "%s: sensor '%s'\n",
  194. dev_name(data->hwmon_dev), client->name);
  195. return 0;
  196. exit_remove:
  197. sysfs_remove_group(&client->dev.kobj, &lm73_group);
  198. return status;
  199. }
  200. static int lm73_remove(struct i2c_client *client)
  201. {
  202. struct lm73_data *data = i2c_get_clientdata(client);
  203. hwmon_device_unregister(data->hwmon_dev);
  204. sysfs_remove_group(&client->dev.kobj, &lm73_group);
  205. return 0;
  206. }
  207. static const struct i2c_device_id lm73_ids[] = {
  208. { "lm73", 0 },
  209. { /* LIST END */ }
  210. };
  211. MODULE_DEVICE_TABLE(i2c, lm73_ids);
  212. /* Return 0 if detection is successful, -ENODEV otherwise */
  213. static int lm73_detect(struct i2c_client *new_client,
  214. struct i2c_board_info *info)
  215. {
  216. struct i2c_adapter *adapter = new_client->adapter;
  217. int id, ctrl, conf;
  218. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  219. I2C_FUNC_SMBUS_WORD_DATA))
  220. return -ENODEV;
  221. /*
  222. * Do as much detection as possible with byte reads first, as word
  223. * reads can confuse other devices.
  224. */
  225. ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
  226. if (ctrl < 0 || (ctrl & 0x10))
  227. return -ENODEV;
  228. conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF);
  229. if (conf < 0 || (conf & 0x0c))
  230. return -ENODEV;
  231. id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID);
  232. if (id < 0 || id != (LM73_ID & 0xff))
  233. return -ENODEV;
  234. /* Check device ID */
  235. id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
  236. if (id < 0 || id != LM73_ID)
  237. return -ENODEV;
  238. strlcpy(info->type, "lm73", I2C_NAME_SIZE);
  239. return 0;
  240. }
  241. static struct i2c_driver lm73_driver = {
  242. .class = I2C_CLASS_HWMON,
  243. .driver = {
  244. .name = "lm73",
  245. },
  246. .probe = lm73_probe,
  247. .remove = lm73_remove,
  248. .id_table = lm73_ids,
  249. .detect = lm73_detect,
  250. .address_list = normal_i2c,
  251. };
  252. module_i2c_driver(lm73_driver);
  253. MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
  254. MODULE_DESCRIPTION("LM73 driver");
  255. MODULE_LICENSE("GPL");