lm73.c 7.3 KB

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