lm70.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * lm70.c
  4. *
  5. * The LM70 is a temperature sensor chip from National Semiconductor (NS).
  6. * Copyright (C) 2006 Kaiwan N Billimoria <kaiwan@designergraphix.com>
  7. *
  8. * The LM70 communicates with a host processor via an SPI/Microwire Bus
  9. * interface. The complete datasheet is available at National's website
  10. * here:
  11. * http://www.national.com/pf/LM/LM70.html
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/device.h>
  18. #include <linux/err.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/mutex.h>
  22. #include <linux/mod_devicetable.h>
  23. #include <linux/of.h>
  24. #include <linux/property.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/slab.h>
  27. #define DRVNAME "lm70"
  28. #define LM70_CHIP_LM70 0 /* original NS LM70 */
  29. #define LM70_CHIP_TMP121 1 /* TI TMP121/TMP123 */
  30. #define LM70_CHIP_LM71 2 /* NS LM71 */
  31. #define LM70_CHIP_LM74 3 /* NS LM74 */
  32. #define LM70_CHIP_TMP122 4 /* TI TMP122/TMP124 */
  33. struct lm70 {
  34. struct spi_device *spi;
  35. struct mutex lock;
  36. unsigned int chip;
  37. };
  38. /* sysfs hook function */
  39. static ssize_t temp1_input_show(struct device *dev,
  40. struct device_attribute *attr, char *buf)
  41. {
  42. struct lm70 *p_lm70 = dev_get_drvdata(dev);
  43. struct spi_device *spi = p_lm70->spi;
  44. int status, val = 0;
  45. u8 rxbuf[2];
  46. s16 raw = 0;
  47. if (mutex_lock_interruptible(&p_lm70->lock))
  48. return -ERESTARTSYS;
  49. /*
  50. * spi_read() requires a DMA-safe buffer; so we use
  51. * spi_write_then_read(), transmitting 0 bytes.
  52. */
  53. status = spi_write_then_read(spi, NULL, 0, &rxbuf[0], 2);
  54. if (status < 0) {
  55. dev_warn(dev, "spi_write_then_read failed with status %d\n",
  56. status);
  57. goto out;
  58. }
  59. raw = (rxbuf[0] << 8) + rxbuf[1];
  60. dev_dbg(dev, "rxbuf[0] : 0x%02x rxbuf[1] : 0x%02x raw=0x%04x\n",
  61. rxbuf[0], rxbuf[1], raw);
  62. /*
  63. * LM70:
  64. * The "raw" temperature read into rxbuf[] is a 16-bit signed 2's
  65. * complement value. Only the MSB 11 bits (1 sign + 10 temperature
  66. * bits) are meaningful; the LSB 5 bits are to be discarded.
  67. * See the datasheet.
  68. *
  69. * Further, each bit represents 0.25 degrees Celsius; so, multiply
  70. * by 0.25. Also multiply by 1000 to represent in millidegrees
  71. * Celsius.
  72. * So it's equivalent to multiplying by 0.25 * 1000 = 250.
  73. *
  74. * LM74 and TMP121/TMP122/TMP123/TMP124:
  75. * 13 bits of 2's complement data, discard LSB 3 bits,
  76. * resolution 0.0625 degrees celsius.
  77. *
  78. * LM71:
  79. * 14 bits of 2's complement data, discard LSB 2 bits,
  80. * resolution 0.0312 degrees celsius.
  81. */
  82. switch (p_lm70->chip) {
  83. case LM70_CHIP_LM70:
  84. val = ((int)raw / 32) * 250;
  85. break;
  86. case LM70_CHIP_TMP121:
  87. case LM70_CHIP_TMP122:
  88. case LM70_CHIP_LM74:
  89. val = ((int)raw / 8) * 625 / 10;
  90. break;
  91. case LM70_CHIP_LM71:
  92. val = ((int)raw / 4) * 3125 / 100;
  93. break;
  94. }
  95. status = sprintf(buf, "%d\n", val); /* millidegrees Celsius */
  96. out:
  97. mutex_unlock(&p_lm70->lock);
  98. return status;
  99. }
  100. static DEVICE_ATTR_RO(temp1_input);
  101. static struct attribute *lm70_attrs[] = {
  102. &dev_attr_temp1_input.attr,
  103. NULL
  104. };
  105. ATTRIBUTE_GROUPS(lm70);
  106. /*----------------------------------------------------------------------*/
  107. #ifdef CONFIG_OF
  108. static const struct of_device_id lm70_of_ids[] = {
  109. {
  110. .compatible = "ti,lm70",
  111. .data = (void *) LM70_CHIP_LM70,
  112. },
  113. {
  114. .compatible = "ti,tmp121",
  115. .data = (void *) LM70_CHIP_TMP121,
  116. },
  117. {
  118. .compatible = "ti,tmp122",
  119. .data = (void *) LM70_CHIP_TMP122,
  120. },
  121. {
  122. .compatible = "ti,lm71",
  123. .data = (void *) LM70_CHIP_LM71,
  124. },
  125. {
  126. .compatible = "ti,lm74",
  127. .data = (void *) LM70_CHIP_LM74,
  128. },
  129. {},
  130. };
  131. MODULE_DEVICE_TABLE(of, lm70_of_ids);
  132. #endif
  133. static int lm70_probe(struct spi_device *spi)
  134. {
  135. struct device *hwmon_dev;
  136. struct lm70 *p_lm70;
  137. int chip;
  138. if (dev_fwnode(&spi->dev))
  139. chip = (int)(uintptr_t)device_get_match_data(&spi->dev);
  140. else
  141. chip = spi_get_device_id(spi)->driver_data;
  142. /* signaling is SPI_MODE_0 */
  143. if (spi->mode & (SPI_CPOL | SPI_CPHA))
  144. return -EINVAL;
  145. /* NOTE: we assume 8-bit words, and convert to 16 bits manually */
  146. p_lm70 = devm_kzalloc(&spi->dev, sizeof(*p_lm70), GFP_KERNEL);
  147. if (!p_lm70)
  148. return -ENOMEM;
  149. mutex_init(&p_lm70->lock);
  150. p_lm70->chip = chip;
  151. p_lm70->spi = spi;
  152. hwmon_dev = devm_hwmon_device_register_with_groups(&spi->dev,
  153. spi->modalias,
  154. p_lm70, lm70_groups);
  155. return PTR_ERR_OR_ZERO(hwmon_dev);
  156. }
  157. static const struct spi_device_id lm70_ids[] = {
  158. { "lm70", LM70_CHIP_LM70 },
  159. { "tmp121", LM70_CHIP_TMP121 },
  160. { "tmp122", LM70_CHIP_TMP122 },
  161. { "lm71", LM70_CHIP_LM71 },
  162. { "lm74", LM70_CHIP_LM74 },
  163. { },
  164. };
  165. MODULE_DEVICE_TABLE(spi, lm70_ids);
  166. static struct spi_driver lm70_driver = {
  167. .driver = {
  168. .name = "lm70",
  169. .of_match_table = of_match_ptr(lm70_of_ids),
  170. },
  171. .id_table = lm70_ids,
  172. .probe = lm70_probe,
  173. };
  174. module_spi_driver(lm70_driver);
  175. MODULE_AUTHOR("Kaiwan N Billimoria");
  176. MODULE_DESCRIPTION("NS LM70 and compatibles Linux driver");
  177. MODULE_LICENSE("GPL");