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 26static 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 55static int adt7316_spi_multi_write(void *client, u8 reg, u8 count, u8 *data) 56{ 57 struct spi_device *spi_dev = client; 58 u8 buf[ADT7316_REG_MAX_ADDR + 2]; 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 78static int adt7316_spi_read(void *client, u8 reg, u8 *data) 79{ 80 return adt7316_spi_multi_read(client, reg, 1, data); 81} 82 83static 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 92static int 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 119static const struct spi_device_id adt7316_spi_id[] = { 120 { "adt7316", 0 }, 121 { "adt7317", 0 }, 122 { "adt7318", 0 }, 123 { "adt7516", 0 }, 124 { "adt7517", 0 }, 125 { "adt7519", 0 }, 126 { } 127}; 128 129MODULE_DEVICE_TABLE(spi, adt7316_spi_id); 130 131static struct spi_driver adt7316_driver = { 132 .driver = { 133 .name = "adt7316", 134 .pm = ADT7316_PM_OPS, 135 }, 136 .probe = adt7316_spi_probe, 137 .id_table = adt7316_spi_id, 138}; 139module_spi_driver(adt7316_driver); 140 141MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>"); 142MODULE_DESCRIPTION("SPI bus driver for Analog Devices ADT7316/7/8 and ADT7516/7/9 digital temperature sensor, ADC and DAC"); 143MODULE_LICENSE("GPL v2"); 144