2 * API bus driver for ADT7316/7/8 ADT7516/7/9 digital temperature
5 * Copyright 2010 Analog Devices Inc.
7 * Licensed under the GPL-2 or later.
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/spi/spi.h>
18 #define ADT7316_SPI_MAX_FREQ_HZ 5000000
19 #define ADT7316_SPI_CMD_READ 0x91
20 #define ADT7316_SPI_CMD_WRITE 0x90
23 * adt7316 register access by SPI
26 static int adt7316_spi_multi_read(void *client
, u8 reg
, u8 count
, u8
*data
)
28 struct spi_device
*spi_dev
= client
;
32 if (count
> ADT7316_REG_MAX_ADDR
)
33 count
= ADT7316_REG_MAX_ADDR
;
35 cmd
[0] = ADT7316_SPI_CMD_WRITE
;
38 ret
= spi_write(spi_dev
, cmd
, 2);
40 dev_err(&spi_dev
->dev
, "SPI fail to select reg\n");
44 cmd
[0] = ADT7316_SPI_CMD_READ
;
46 ret
= spi_write_then_read(spi_dev
, cmd
, 1, data
, count
);
48 dev_err(&spi_dev
->dev
, "SPI read data error\n");
55 static int adt7316_spi_multi_write(void *client
, u8 reg
, u8 count
, u8
*data
)
57 struct spi_device
*spi_dev
= client
;
58 u8 buf
[ADT7316_REG_MAX_ADDR
+ 2];
61 if (count
> ADT7316_REG_MAX_ADDR
)
62 count
= ADT7316_REG_MAX_ADDR
;
64 buf
[0] = ADT7316_SPI_CMD_WRITE
;
66 for (i
= 0; i
< count
; i
++)
69 ret
= spi_write(spi_dev
, buf
, count
+ 2);
71 dev_err(&spi_dev
->dev
, "SPI write error\n");
78 static int adt7316_spi_read(void *client
, u8 reg
, u8
*data
)
80 return adt7316_spi_multi_read(client
, reg
, 1, data
);
83 static int adt7316_spi_write(void *client
, u8 reg
, u8 val
)
85 return adt7316_spi_multi_write(client
, reg
, 1, &val
);
89 * device probe and remove
92 static int __devinit
adt7316_spi_probe(struct spi_device
*spi_dev
)
94 struct adt7316_bus bus
= {
97 .irq_flags
= IRQF_TRIGGER_LOW
,
98 .read
= adt7316_spi_read
,
99 .write
= adt7316_spi_write
,
100 .multi_read
= adt7316_spi_multi_read
,
101 .multi_write
= adt7316_spi_multi_write
,
104 /* don't exceed max specified SPI CLK frequency */
105 if (spi_dev
->max_speed_hz
> ADT7316_SPI_MAX_FREQ_HZ
) {
106 dev_err(&spi_dev
->dev
, "SPI CLK %d Hz?\n",
107 spi_dev
->max_speed_hz
);
111 /* switch from default I2C protocol to SPI protocol */
112 adt7316_spi_write(spi_dev
, 0, 0);
113 adt7316_spi_write(spi_dev
, 0, 0);
114 adt7316_spi_write(spi_dev
, 0, 0);
116 return adt7316_probe(&spi_dev
->dev
, &bus
, spi_dev
->modalias
);
119 static int __devexit
adt7316_spi_remove(struct spi_device
*spi_dev
)
121 return adt7316_remove(&spi_dev
->dev
);
124 static const struct spi_device_id adt7316_spi_id
[] = {
134 MODULE_DEVICE_TABLE(spi
, adt7316_spi_id
);
137 static int adt7316_spi_suspend(struct spi_device
*spi_dev
, pm_message_t message
)
139 return adt7316_disable(&spi_dev
->dev
);
142 static int adt7316_spi_resume(struct spi_device
*spi_dev
)
144 return adt7316_enable(&spi_dev
->dev
);
147 # define adt7316_spi_suspend NULL
148 # define adt7316_spi_resume NULL
151 static struct spi_driver adt7316_driver
= {
154 .owner
= THIS_MODULE
,
156 .probe
= adt7316_spi_probe
,
157 .remove
= __devexit_p(adt7316_spi_remove
),
158 .suspend
= adt7316_spi_suspend
,
159 .resume
= adt7316_spi_resume
,
160 .id_table
= adt7316_spi_id
,
162 module_spi_driver(adt7316_driver
);
164 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
165 MODULE_DESCRIPTION("SPI bus driver for Analog Devices ADT7316/7/8 and"
166 "ADT7516/7/9 digital temperature sensor, ADC and DAC");
167 MODULE_LICENSE("GPL v2");