Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / staging / iio / addac / adt7316-spi.c
blob1ea3cd06299de93af117900c7e91fc40fbbefc25
1 /*
2 * API bus driver for ADT7316/7/8 ADT7516/7/9 digital temperature
3 * sensor, ADC and DAC
5 * Copyright 2010 Analog Devices Inc.
7 * Licensed under the GPL-2 or later.
8 */
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>
16 #include "adt7316.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;
29 u8 cmd[2];
30 int ret = 0;
32 if (count > ADT7316_REG_MAX_ADDR)
33 count = ADT7316_REG_MAX_ADDR;
35 cmd[0] = ADT7316_SPI_CMD_WRITE;
36 cmd[1] = reg;
38 ret = spi_write(spi_dev, cmd, 2);
39 if (ret < 0) {
40 dev_err(&spi_dev->dev, "SPI fail to select reg\n");
41 return ret;
44 cmd[0] = ADT7316_SPI_CMD_READ;
46 ret = spi_write_then_read(spi_dev, cmd, 1, data, count);
47 if (ret < 0) {
48 dev_err(&spi_dev->dev, "SPI read data error\n");
49 return ret;
52 return 0;
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];
59 int i, ret = 0;
61 if (count > ADT7316_REG_MAX_ADDR)
62 count = ADT7316_REG_MAX_ADDR;
64 buf[0] = ADT7316_SPI_CMD_WRITE;
65 buf[1] = reg;
66 for (i = 0; i < count; i++)
67 buf[i + 2] = data[i];
69 ret = spi_write(spi_dev, buf, count + 2);
70 if (ret < 0) {
71 dev_err(&spi_dev->dev, "SPI write error\n");
72 return ret;
75 return ret;
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 = {
95 .client = spi_dev,
96 .irq = spi_dev->irq,
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);
108 return -EINVAL;
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[] = {
125 { "adt7316", 0 },
126 { "adt7317", 0 },
127 { "adt7318", 0 },
128 { "adt7516", 0 },
129 { "adt7517", 0 },
130 { "adt7519", 0 },
134 MODULE_DEVICE_TABLE(spi, adt7316_spi_id);
136 #ifdef CONFIG_PM
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);
146 #else
147 # define adt7316_spi_suspend NULL
148 # define adt7316_spi_resume NULL
149 #endif
151 static struct spi_driver adt7316_driver = {
152 .driver = {
153 .name = "adt7316",
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");