Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Analog Devices digital potentiometers (SPI bus) |
| 3 | * |
Michael Hennerich | 7f3379d | 2011-11-18 11:05:11 +0100 | [diff] [blame] | 4 | * Copyright (C) 2010-2011 Michael Hennerich, Analog Devices Inc. |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 5 | * |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/spi/spi.h> |
| 10 | #include <linux/module.h> |
| 11 | |
| 12 | #include "ad525x_dpot.h" |
| 13 | |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 14 | /* SPI bus functions */ |
| 15 | static int write8(void *client, u8 val) |
| 16 | { |
| 17 | u8 data = val; |
Mohammad Jamal | c076860 | 2014-12-18 07:24:11 +0530 | [diff] [blame] | 18 | |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 19 | return spi_write(client, &data, 1); |
| 20 | } |
| 21 | |
| 22 | static int write16(void *client, u8 reg, u8 val) |
| 23 | { |
| 24 | u8 data[2] = {reg, val}; |
Mohammad Jamal | c076860 | 2014-12-18 07:24:11 +0530 | [diff] [blame] | 25 | |
Michael Hennerich | 1f9fa52 | 2010-10-26 14:21:16 -0700 | [diff] [blame] | 26 | return spi_write(client, data, 2); |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | static int write24(void *client, u8 reg, u16 val) |
| 30 | { |
| 31 | u8 data[3] = {reg, val >> 8, val}; |
Mohammad Jamal | c076860 | 2014-12-18 07:24:11 +0530 | [diff] [blame] | 32 | |
Michael Hennerich | 1f9fa52 | 2010-10-26 14:21:16 -0700 | [diff] [blame] | 33 | return spi_write(client, data, 3); |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static int read8(void *client) |
| 37 | { |
| 38 | int ret; |
| 39 | u8 data; |
Mohammad Jamal | c076860 | 2014-12-18 07:24:11 +0530 | [diff] [blame] | 40 | |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 41 | ret = spi_read(client, &data, 1); |
| 42 | if (ret < 0) |
| 43 | return ret; |
| 44 | |
| 45 | return data; |
| 46 | } |
| 47 | |
| 48 | static int read16(void *client, u8 reg) |
| 49 | { |
| 50 | int ret; |
| 51 | u8 buf_rx[2]; |
| 52 | |
| 53 | write16(client, reg, 0); |
| 54 | ret = spi_read(client, buf_rx, 2); |
| 55 | if (ret < 0) |
| 56 | return ret; |
| 57 | |
| 58 | return (buf_rx[0] << 8) | buf_rx[1]; |
| 59 | } |
| 60 | |
| 61 | static int read24(void *client, u8 reg) |
| 62 | { |
| 63 | int ret; |
| 64 | u8 buf_rx[3]; |
| 65 | |
| 66 | write24(client, reg, 0); |
| 67 | ret = spi_read(client, buf_rx, 3); |
| 68 | if (ret < 0) |
| 69 | return ret; |
| 70 | |
| 71 | return (buf_rx[1] << 8) | buf_rx[2]; |
| 72 | } |
| 73 | |
| 74 | static const struct ad_dpot_bus_ops bops = { |
| 75 | .read_d8 = read8, |
| 76 | .read_r8d8 = read16, |
| 77 | .read_r8d16 = read24, |
| 78 | .write_d8 = write8, |
| 79 | .write_r8d8 = write16, |
| 80 | .write_r8d16 = write24, |
| 81 | }; |
Bill Pemberton | 80c8ae2 | 2012-11-19 13:23:05 -0500 | [diff] [blame] | 82 | static int ad_dpot_spi_probe(struct spi_device *spi) |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 83 | { |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 84 | struct ad_dpot_bus_data bdata = { |
| 85 | .client = spi, |
| 86 | .bops = &bops, |
| 87 | }; |
| 88 | |
Michael Hennerich | 7f3379d | 2011-11-18 11:05:11 +0100 | [diff] [blame] | 89 | return ad_dpot_probe(&spi->dev, &bdata, |
| 90 | spi_get_device_id(spi)->driver_data, |
| 91 | spi_get_device_id(spi)->name); |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Bill Pemberton | 486a5c2 | 2012-11-19 13:26:02 -0500 | [diff] [blame] | 94 | static int ad_dpot_spi_remove(struct spi_device *spi) |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 95 | { |
| 96 | return ad_dpot_remove(&spi->dev); |
| 97 | } |
| 98 | |
Michael Hennerich | 7f3379d | 2011-11-18 11:05:11 +0100 | [diff] [blame] | 99 | static const struct spi_device_id ad_dpot_spi_id[] = { |
| 100 | {"ad5160", AD5160_ID}, |
| 101 | {"ad5161", AD5161_ID}, |
| 102 | {"ad5162", AD5162_ID}, |
| 103 | {"ad5165", AD5165_ID}, |
| 104 | {"ad5200", AD5200_ID}, |
| 105 | {"ad5201", AD5201_ID}, |
| 106 | {"ad5203", AD5203_ID}, |
| 107 | {"ad5204", AD5204_ID}, |
| 108 | {"ad5206", AD5206_ID}, |
| 109 | {"ad5207", AD5207_ID}, |
| 110 | {"ad5231", AD5231_ID}, |
| 111 | {"ad5232", AD5232_ID}, |
| 112 | {"ad5233", AD5233_ID}, |
| 113 | {"ad5235", AD5235_ID}, |
| 114 | {"ad5260", AD5260_ID}, |
| 115 | {"ad5262", AD5262_ID}, |
| 116 | {"ad5263", AD5263_ID}, |
| 117 | {"ad5290", AD5290_ID}, |
| 118 | {"ad5291", AD5291_ID}, |
| 119 | {"ad5292", AD5292_ID}, |
| 120 | {"ad5293", AD5293_ID}, |
| 121 | {"ad7376", AD7376_ID}, |
| 122 | {"ad8400", AD8400_ID}, |
| 123 | {"ad8402", AD8402_ID}, |
| 124 | {"ad8403", AD8403_ID}, |
| 125 | {"adn2850", ADN2850_ID}, |
| 126 | {"ad5270", AD5270_ID}, |
| 127 | {"ad5271", AD5271_ID}, |
| 128 | {} |
| 129 | }; |
| 130 | MODULE_DEVICE_TABLE(spi, ad_dpot_spi_id); |
| 131 | |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 132 | static struct spi_driver ad_dpot_spi_driver = { |
| 133 | .driver = { |
| 134 | .name = "ad_dpot", |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 135 | }, |
| 136 | .probe = ad_dpot_spi_probe, |
Bill Pemberton | 2d6bed9 | 2012-11-19 13:21:23 -0500 | [diff] [blame] | 137 | .remove = ad_dpot_spi_remove, |
Michael Hennerich | 7f3379d | 2011-11-18 11:05:11 +0100 | [diff] [blame] | 138 | .id_table = ad_dpot_spi_id, |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Axel Lin | a3dc3c9 | 2012-01-22 15:38:22 +0800 | [diff] [blame] | 141 | module_spi_driver(ad_dpot_spi_driver); |
Michael Hennerich | 6c536e4 | 2010-05-24 14:33:14 -0700 | [diff] [blame] | 142 | |
| 143 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 144 | MODULE_DESCRIPTION("digital potentiometer SPI bus driver"); |
| 145 | MODULE_LICENSE("GPL"); |
| 146 | MODULE_ALIAS("spi:ad_dpot"); |