adt7310.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * ADT7310/ADT7310 digital temperature sensor driver
  3. *
  4. * Copyright 2012-2013 Analog Devices Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/spi/spi.h>
  12. #include <asm/unaligned.h>
  13. #include "adt7x10.h"
  14. #define ADT7310_STATUS 0
  15. #define ADT7310_CONFIG 1
  16. #define ADT7310_TEMPERATURE 2
  17. #define ADT7310_ID 3
  18. #define ADT7310_T_CRIT 4
  19. #define ADT7310_T_HYST 5
  20. #define ADT7310_T_ALARM_HIGH 6
  21. #define ADT7310_T_ALARM_LOW 7
  22. static const u8 adt7310_reg_table[] = {
  23. [ADT7X10_TEMPERATURE] = ADT7310_TEMPERATURE,
  24. [ADT7X10_STATUS] = ADT7310_STATUS,
  25. [ADT7X10_CONFIG] = ADT7310_CONFIG,
  26. [ADT7X10_T_ALARM_HIGH] = ADT7310_T_ALARM_HIGH,
  27. [ADT7X10_T_ALARM_LOW] = ADT7310_T_ALARM_LOW,
  28. [ADT7X10_T_CRIT] = ADT7310_T_CRIT,
  29. [ADT7X10_T_HYST] = ADT7310_T_HYST,
  30. [ADT7X10_ID] = ADT7310_ID,
  31. };
  32. #define ADT7310_CMD_REG_OFFSET 3
  33. #define ADT7310_CMD_READ 0x40
  34. #define AD7310_COMMAND(reg) (adt7310_reg_table[(reg)] << ADT7310_CMD_REG_OFFSET)
  35. static int adt7310_spi_read_word(struct device *dev, u8 reg)
  36. {
  37. struct spi_device *spi = to_spi_device(dev);
  38. return spi_w8r16be(spi, AD7310_COMMAND(reg) | ADT7310_CMD_READ);
  39. }
  40. static int adt7310_spi_write_word(struct device *dev, u8 reg, u16 data)
  41. {
  42. struct spi_device *spi = to_spi_device(dev);
  43. u8 buf[3];
  44. buf[0] = AD7310_COMMAND(reg);
  45. put_unaligned_be16(data, &buf[1]);
  46. return spi_write(spi, buf, sizeof(buf));
  47. }
  48. static int adt7310_spi_read_byte(struct device *dev, u8 reg)
  49. {
  50. struct spi_device *spi = to_spi_device(dev);
  51. return spi_w8r8(spi, AD7310_COMMAND(reg) | ADT7310_CMD_READ);
  52. }
  53. static int adt7310_spi_write_byte(struct device *dev, u8 reg,
  54. u8 data)
  55. {
  56. struct spi_device *spi = to_spi_device(dev);
  57. u8 buf[2];
  58. buf[0] = AD7310_COMMAND(reg);
  59. buf[1] = data;
  60. return spi_write(spi, buf, sizeof(buf));
  61. }
  62. static const struct adt7x10_ops adt7310_spi_ops = {
  63. .read_word = adt7310_spi_read_word,
  64. .write_word = adt7310_spi_write_word,
  65. .read_byte = adt7310_spi_read_byte,
  66. .write_byte = adt7310_spi_write_byte,
  67. };
  68. static int adt7310_spi_probe(struct spi_device *spi)
  69. {
  70. return adt7x10_probe(&spi->dev, spi_get_device_id(spi)->name, spi->irq,
  71. &adt7310_spi_ops);
  72. }
  73. static int adt7310_spi_remove(struct spi_device *spi)
  74. {
  75. return adt7x10_remove(&spi->dev, spi->irq);
  76. }
  77. static const struct spi_device_id adt7310_id[] = {
  78. { "adt7310", 0 },
  79. { "adt7320", 0 },
  80. {}
  81. };
  82. MODULE_DEVICE_TABLE(spi, adt7310_id);
  83. static struct spi_driver adt7310_driver = {
  84. .driver = {
  85. .name = "adt7310",
  86. .pm = ADT7X10_DEV_PM_OPS,
  87. },
  88. .probe = adt7310_spi_probe,
  89. .remove = adt7310_spi_remove,
  90. .id_table = adt7310_id,
  91. };
  92. module_spi_driver(adt7310_driver);
  93. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  94. MODULE_DESCRIPTION("ADT7310/ADT7320 driver");
  95. MODULE_LICENSE("GPL");