Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adt7316-spi.c
Go to the documentation of this file.
1 /*
2  * API bus driver for ADT7316/7/8 ADT7516/7/9 digital temperature
3  * sensor, ADC and DAC
4  *
5  * Copyright 2010 Analog Devices Inc.
6  *
7  * Licensed under the GPL-2 or later.
8  */
9 
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>
15 
16 #include "adt7316.h"
17 
18 #define ADT7316_SPI_MAX_FREQ_HZ 5000000
19 #define ADT7316_SPI_CMD_READ 0x91
20 #define ADT7316_SPI_CMD_WRITE 0x90
21 
22 /*
23  * adt7316 register access by SPI
24  */
25 
26 static int adt7316_spi_multi_read(void *client, u8 reg, u8 count, u8 *data)
27 {
28  struct spi_device *spi_dev = client;
29  u8 cmd[2];
30  int ret = 0;
31 
32  if (count > ADT7316_REG_MAX_ADDR)
33  count = ADT7316_REG_MAX_ADDR;
34 
35  cmd[0] = ADT7316_SPI_CMD_WRITE;
36  cmd[1] = reg;
37 
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;
42  }
43 
44  cmd[0] = ADT7316_SPI_CMD_READ;
45 
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;
50  }
51 
52  return 0;
53 }
54 
55 static int adt7316_spi_multi_write(void *client, u8 reg, u8 count, u8 *data)
56 {
57  struct spi_device *spi_dev = client;
59  int i, ret = 0;
60 
61  if (count > ADT7316_REG_MAX_ADDR)
62  count = ADT7316_REG_MAX_ADDR;
63 
64  buf[0] = ADT7316_SPI_CMD_WRITE;
65  buf[1] = reg;
66  for (i = 0; i < count; i++)
67  buf[i + 2] = data[i];
68 
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;
73  }
74 
75  return ret;
76 }
77 
78 static int adt7316_spi_read(void *client, u8 reg, u8 *data)
79 {
80  return adt7316_spi_multi_read(client, reg, 1, data);
81 }
82 
83 static int adt7316_spi_write(void *client, u8 reg, u8 val)
84 {
85  return adt7316_spi_multi_write(client, reg, 1, &val);
86 }
87 
88 /*
89  * device probe and remove
90  */
91 
92 static int __devinit adt7316_spi_probe(struct spi_device *spi_dev)
93 {
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,
102  };
103 
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;
109  }
110 
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);
115 
116  return adt7316_probe(&spi_dev->dev, &bus, spi_dev->modalias);
117 }
118 
119 static int __devexit adt7316_spi_remove(struct spi_device *spi_dev)
120 {
121  return adt7316_remove(&spi_dev->dev);
122 }
123 
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 },
131  { }
132 };
133 
134 MODULE_DEVICE_TABLE(spi, adt7316_spi_id);
135 
136 static struct spi_driver adt7316_driver = {
137  .driver = {
138  .name = "adt7316",
139  .pm = ADT7316_PM_OPS,
140  .owner = THIS_MODULE,
141  },
142  .probe = adt7316_spi_probe,
143  .remove = __devexit_p(adt7316_spi_remove),
144  .id_table = adt7316_spi_id,
145 };
146 module_spi_driver(adt7316_driver);
147 
148 MODULE_AUTHOR("Sonic Zhang <[email protected]>");
149 MODULE_DESCRIPTION("SPI bus driver for Analog Devices ADT7316/7/8 and"
150  "ADT7516/7/9 digital temperature sensor, ADC and DAC");
151 MODULE_LICENSE("GPL v2");