blob: c40cac16af68385b81eabbba22d0453991afbea7 [file] [log] [blame]
Thomas Gleixner80503b22019-05-24 12:04:09 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Lars-Peter Clausen51c2a482013-03-12 11:38:46 +01002/*
3 * ADT7310/ADT7310 digital temperature sensor driver
4 *
5 * Copyright 2012-2013 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
Lars-Peter Clausen51c2a482013-03-12 11:38:46 +01007 */
8
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/spi/spi.h>
12#include <asm/unaligned.h>
13
14#include "adt7x10.h"
15
16#define ADT7310_STATUS 0
17#define ADT7310_CONFIG 1
18#define ADT7310_TEMPERATURE 2
19#define ADT7310_ID 3
20#define ADT7310_T_CRIT 4
21#define ADT7310_T_HYST 5
22#define ADT7310_T_ALARM_HIGH 6
23#define ADT7310_T_ALARM_LOW 7
24
25static const u8 adt7310_reg_table[] = {
26 [ADT7X10_TEMPERATURE] = ADT7310_TEMPERATURE,
27 [ADT7X10_STATUS] = ADT7310_STATUS,
28 [ADT7X10_CONFIG] = ADT7310_CONFIG,
29 [ADT7X10_T_ALARM_HIGH] = ADT7310_T_ALARM_HIGH,
30 [ADT7X10_T_ALARM_LOW] = ADT7310_T_ALARM_LOW,
31 [ADT7X10_T_CRIT] = ADT7310_T_CRIT,
32 [ADT7X10_T_HYST] = ADT7310_T_HYST,
33 [ADT7X10_ID] = ADT7310_ID,
34};
35
36#define ADT7310_CMD_REG_OFFSET 3
37#define ADT7310_CMD_READ 0x40
38
39#define AD7310_COMMAND(reg) (adt7310_reg_table[(reg)] << ADT7310_CMD_REG_OFFSET)
40
41static int adt7310_spi_read_word(struct device *dev, u8 reg)
42{
43 struct spi_device *spi = to_spi_device(dev);
Lars-Peter Clausen51c2a482013-03-12 11:38:46 +010044
Lars-Peter Clausen600ba982013-09-27 16:34:28 +020045 return spi_w8r16be(spi, AD7310_COMMAND(reg) | ADT7310_CMD_READ);
Lars-Peter Clausen51c2a482013-03-12 11:38:46 +010046}
47
48static int adt7310_spi_write_word(struct device *dev, u8 reg, u16 data)
49{
50 struct spi_device *spi = to_spi_device(dev);
51 u8 buf[3];
52
53 buf[0] = AD7310_COMMAND(reg);
54 put_unaligned_be16(data, &buf[1]);
55
56 return spi_write(spi, buf, sizeof(buf));
57}
58
59static int adt7310_spi_read_byte(struct device *dev, u8 reg)
60{
61 struct spi_device *spi = to_spi_device(dev);
62
63 return spi_w8r8(spi, AD7310_COMMAND(reg) | ADT7310_CMD_READ);
64}
65
66static int adt7310_spi_write_byte(struct device *dev, u8 reg,
67 u8 data)
68{
69 struct spi_device *spi = to_spi_device(dev);
70 u8 buf[2];
71
72 buf[0] = AD7310_COMMAND(reg);
73 buf[1] = data;
74
75 return spi_write(spi, buf, sizeof(buf));
76}
77
78static const struct adt7x10_ops adt7310_spi_ops = {
79 .read_word = adt7310_spi_read_word,
80 .write_word = adt7310_spi_write_word,
81 .read_byte = adt7310_spi_read_byte,
82 .write_byte = adt7310_spi_write_byte,
83};
84
85static int adt7310_spi_probe(struct spi_device *spi)
86{
Lars-Peter Clausen4b5e5362013-03-12 11:38:47 +010087 return adt7x10_probe(&spi->dev, spi_get_device_id(spi)->name, spi->irq,
Lars-Peter Clausen51c2a482013-03-12 11:38:46 +010088 &adt7310_spi_ops);
89}
90
91static int adt7310_spi_remove(struct spi_device *spi)
92{
Uwe Kleine-Königbeee7892021-10-11 15:27:43 +020093 adt7x10_remove(&spi->dev, spi->irq);
94 return 0;
Lars-Peter Clausen51c2a482013-03-12 11:38:46 +010095}
96
97static const struct spi_device_id adt7310_id[] = {
98 { "adt7310", 0 },
99 { "adt7320", 0 },
100 {}
101};
102MODULE_DEVICE_TABLE(spi, adt7310_id);
103
104static struct spi_driver adt7310_driver = {
105 .driver = {
106 .name = "adt7310",
Lars-Peter Clausen51c2a482013-03-12 11:38:46 +0100107 .pm = ADT7X10_DEV_PM_OPS,
108 },
109 .probe = adt7310_spi_probe,
110 .remove = adt7310_spi_remove,
111 .id_table = adt7310_id,
112};
113module_spi_driver(adt7310_driver);
114
115MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
116MODULE_DESCRIPTION("ADT7310/ADT7320 driver");
117MODULE_LICENSE("GPL");