lm73.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/slab.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. /* Insmod parameters */
  27. I2C_CLIENT_INSMOD_1(lm73);
  28. /* LM73 registers */
  29. #define LM73_REG_INPUT 0x00
  30. #define LM73_REG_CONF 0x01
  31. #define LM73_REG_MAX 0x02
  32. #define LM73_REG_MIN 0x03
  33. #define LM73_REG_CTRL 0x04
  34. #define LM73_REG_ID 0x07
  35. #define LM73_ID 0x9001 /* or 0x190 after a swab16() */
  36. #define DRVNAME "lm73"
  37. #define LM73_TEMP_MIN (-40)
  38. #define LM73_TEMP_MAX 150
  39. /*-----------------------------------------------------------------------*/
  40. static ssize_t set_temp(struct device *dev, struct device_attribute *da,
  41. const char *buf, size_t count)
  42. {
  43. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  44. struct i2c_client *client = to_i2c_client(dev);
  45. long temp;
  46. short value;
  47. int status = strict_strtol(buf, 10, &temp);
  48. if (status < 0)
  49. return status;
  50. /* Write value */
  51. value = (short) SENSORS_LIMIT(temp/250, (LM73_TEMP_MIN*4),
  52. (LM73_TEMP_MAX*4)) << 5;
  53. i2c_smbus_write_word_data(client, attr->index, swab16(value));
  54. return count;
  55. }
  56. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  57. char *buf)
  58. {
  59. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  60. struct i2c_client *client = to_i2c_client(dev);
  61. /* use integer division instead of equivalent right shift to
  62. guarantee arithmetic shift and preserve the sign */
  63. int temp = ((s16) (swab16(i2c_smbus_read_word_data(client,
  64. attr->index)))*250) / 32;
  65. return sprintf(buf, "%d\n", temp);
  66. }
  67. /*-----------------------------------------------------------------------*/
  68. /* sysfs attributes for hwmon */
  69. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  70. show_temp, set_temp, LM73_REG_MAX);
  71. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  72. show_temp, set_temp, LM73_REG_MIN);
  73. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  74. show_temp, NULL, LM73_REG_INPUT);
  75. static struct attribute *lm73_attributes[] = {
  76. &sensor_dev_attr_temp1_input.dev_attr.attr,
  77. &sensor_dev_attr_temp1_max.dev_attr.attr,
  78. &sensor_dev_attr_temp1_min.dev_attr.attr,
  79. NULL
  80. };
  81. static const struct attribute_group lm73_group = {
  82. .attrs = lm73_attributes,
  83. };
  84. /*-----------------------------------------------------------------------*/
  85. /* device probe and removal */
  86. static int
  87. lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
  88. {
  89. struct device *hwmon_dev;
  90. int status;
  91. /* Register sysfs hooks */
  92. status = sysfs_create_group(&client->dev.kobj, &lm73_group);
  93. if (status)
  94. return status;
  95. hwmon_dev = hwmon_device_register(&client->dev);
  96. if (IS_ERR(hwmon_dev)) {
  97. status = PTR_ERR(hwmon_dev);
  98. goto exit_remove;
  99. }
  100. i2c_set_clientdata(client, hwmon_dev);
  101. dev_info(&client->dev, "%s: sensor '%s'\n",
  102. dev_name(hwmon_dev), client->name);
  103. return 0;
  104. exit_remove:
  105. sysfs_remove_group(&client->dev.kobj, &lm73_group);
  106. return status;
  107. }
  108. static int lm73_remove(struct i2c_client *client)
  109. {
  110. struct device *hwmon_dev = i2c_get_clientdata(client);
  111. hwmon_device_unregister(hwmon_dev);
  112. sysfs_remove_group(&client->dev.kobj, &lm73_group);
  113. i2c_set_clientdata(client, NULL);
  114. return 0;
  115. }
  116. static const struct i2c_device_id lm73_ids[] = {
  117. { "lm73", lm73 },
  118. { /* LIST END */ }
  119. };
  120. MODULE_DEVICE_TABLE(i2c, lm73_ids);
  121. /* Return 0 if detection is successful, -ENODEV otherwise */
  122. static int lm73_detect(struct i2c_client *new_client, int kind,
  123. struct i2c_board_info *info)
  124. {
  125. struct i2c_adapter *adapter = new_client->adapter;
  126. u16 id;
  127. u8 ctrl;
  128. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  129. I2C_FUNC_SMBUS_WORD_DATA))
  130. return -ENODEV;
  131. /* Check device ID */
  132. id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
  133. ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
  134. if ((id != LM73_ID) || (ctrl & 0x10))
  135. return -ENODEV;
  136. strlcpy(info->type, "lm73", I2C_NAME_SIZE);
  137. return 0;
  138. }
  139. static struct i2c_driver lm73_driver = {
  140. .class = I2C_CLASS_HWMON,
  141. .driver = {
  142. .name = "lm73",
  143. },
  144. .probe = lm73_probe,
  145. .remove = lm73_remove,
  146. .id_table = lm73_ids,
  147. .detect = lm73_detect,
  148. .address_data = &addr_data,
  149. };
  150. /* module glue */
  151. static int __init sensors_lm73_init(void)
  152. {
  153. return i2c_add_driver(&lm73_driver);
  154. }
  155. static void __exit sensors_lm73_exit(void)
  156. {
  157. i2c_del_driver(&lm73_driver);
  158. }
  159. MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
  160. MODULE_DESCRIPTION("LM73 driver");
  161. MODULE_LICENSE("GPL");
  162. module_init(sensors_lm73_init);
  163. module_exit(sensors_lm73_exit);