Commit | Line | Data |
---|---|---|
cf35ad61 PM |
1 | /* |
2 | * mcp4725.c - Support for Microchip MCP4725 | |
3 | * | |
4 | * Copyright (C) 2012 Peter Meerwald <pmeerw@pmeerw.net> | |
5 | * | |
6 | * Based on max517 by Roland Stigge <stigge@antcom.de> | |
7 | * | |
8 | * This file is subject to the terms and conditions of version 2 of | |
9 | * the GNU General Public License. See the file COPYING in the main | |
10 | * directory of this archive for more details. | |
11 | * | |
12 | * driver for the Microchip I2C 12-bit digital-to-analog converter (DAC) | |
13 | * (7-bit I2C slave address 0x60, the three LSBs can be configured in | |
14 | * hardware) | |
15 | * | |
16 | * writing the DAC value to EEPROM is not supported | |
17 | */ | |
18 | ||
19 | #include <linux/module.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/i2c.h> | |
22 | #include <linux/err.h> | |
23 | ||
24 | #include <linux/iio/iio.h> | |
25 | #include <linux/iio/sysfs.h> | |
26 | ||
27 | #include <linux/iio/dac/mcp4725.h> | |
28 | ||
29 | #define MCP4725_DRV_NAME "mcp4725" | |
30 | ||
31 | struct mcp4725_data { | |
32 | struct i2c_client *client; | |
33 | u16 vref_mv; | |
34 | u16 dac_value; | |
35 | }; | |
36 | ||
37 | #ifdef CONFIG_PM_SLEEP | |
38 | static int mcp4725_suspend(struct device *dev) | |
39 | { | |
40 | u8 outbuf[2]; | |
41 | ||
42 | outbuf[0] = 0x3 << 4; /* power-down bits, 500 kOhm resistor */ | |
43 | outbuf[1] = 0; | |
44 | ||
45 | return i2c_master_send(to_i2c_client(dev), outbuf, 2); | |
46 | } | |
47 | ||
48 | static int mcp4725_resume(struct device *dev) | |
49 | { | |
50 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); | |
51 | struct mcp4725_data *data = iio_priv(indio_dev); | |
52 | u8 outbuf[2]; | |
53 | ||
54 | /* restore previous DAC value */ | |
55 | outbuf[0] = (data->dac_value >> 8) & 0xf; | |
56 | outbuf[1] = data->dac_value & 0xff; | |
57 | ||
58 | return i2c_master_send(to_i2c_client(dev), outbuf, 2); | |
59 | } | |
60 | ||
61 | static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume); | |
62 | #define MCP4725_PM_OPS (&mcp4725_pm_ops) | |
63 | #else | |
64 | #define MCP4725_PM_OPS NULL | |
65 | #endif | |
66 | ||
67 | static const struct iio_chan_spec mcp4725_channel = { | |
68 | .type = IIO_VOLTAGE, | |
69 | .indexed = 1, | |
70 | .output = 1, | |
71 | .channel = 0, | |
90b46374 JC |
72 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
73 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), | |
cf35ad61 PM |
74 | .scan_type = IIO_ST('u', 12, 16, 0), |
75 | }; | |
76 | ||
77 | static int mcp4725_set_value(struct iio_dev *indio_dev, int val) | |
78 | { | |
79 | struct mcp4725_data *data = iio_priv(indio_dev); | |
80 | u8 outbuf[2]; | |
81 | int ret; | |
82 | ||
83 | if (val >= (1 << 12) || val < 0) | |
84 | return -EINVAL; | |
85 | ||
86 | outbuf[0] = (val >> 8) & 0xf; | |
87 | outbuf[1] = val & 0xff; | |
88 | ||
89 | ret = i2c_master_send(data->client, outbuf, 2); | |
90 | if (ret < 0) | |
91 | return ret; | |
92 | else if (ret != 2) | |
93 | return -EIO; | |
94 | else | |
95 | return 0; | |
96 | } | |
97 | ||
98 | static int mcp4725_read_raw(struct iio_dev *indio_dev, | |
99 | struct iio_chan_spec const *chan, | |
100 | int *val, int *val2, long mask) | |
101 | { | |
102 | struct mcp4725_data *data = iio_priv(indio_dev); | |
103 | unsigned long scale_uv; | |
104 | ||
105 | switch (mask) { | |
106 | case IIO_CHAN_INFO_RAW: | |
107 | *val = data->dac_value; | |
108 | return IIO_VAL_INT; | |
109 | case IIO_CHAN_INFO_SCALE: | |
110 | scale_uv = (data->vref_mv * 1000) >> 12; | |
111 | *val = scale_uv / 1000000; | |
112 | *val2 = scale_uv % 1000000; | |
113 | return IIO_VAL_INT_PLUS_MICRO; | |
114 | } | |
115 | return -EINVAL; | |
116 | } | |
117 | ||
118 | static int mcp4725_write_raw(struct iio_dev *indio_dev, | |
119 | struct iio_chan_spec const *chan, | |
120 | int val, int val2, long mask) | |
121 | { | |
122 | struct mcp4725_data *data = iio_priv(indio_dev); | |
123 | int ret; | |
124 | ||
125 | switch (mask) { | |
126 | case IIO_CHAN_INFO_RAW: | |
127 | ret = mcp4725_set_value(indio_dev, val); | |
128 | data->dac_value = val; | |
129 | break; | |
130 | default: | |
131 | ret = -EINVAL; | |
132 | break; | |
133 | } | |
134 | ||
135 | return ret; | |
136 | } | |
137 | ||
138 | static const struct iio_info mcp4725_info = { | |
139 | .read_raw = mcp4725_read_raw, | |
140 | .write_raw = mcp4725_write_raw, | |
141 | .driver_module = THIS_MODULE, | |
142 | }; | |
143 | ||
fc52692c GKH |
144 | static int mcp4725_probe(struct i2c_client *client, |
145 | const struct i2c_device_id *id) | |
cf35ad61 PM |
146 | { |
147 | struct mcp4725_data *data; | |
148 | struct iio_dev *indio_dev; | |
149 | struct mcp4725_platform_data *platform_data = client->dev.platform_data; | |
150 | u8 inbuf[3]; | |
151 | int err; | |
152 | ||
153 | if (!platform_data || !platform_data->vref_mv) { | |
154 | dev_err(&client->dev, "invalid platform data"); | |
155 | err = -EINVAL; | |
156 | goto exit; | |
157 | } | |
158 | ||
159 | indio_dev = iio_device_alloc(sizeof(*data)); | |
160 | if (indio_dev == NULL) { | |
161 | err = -ENOMEM; | |
162 | goto exit; | |
163 | } | |
164 | data = iio_priv(indio_dev); | |
165 | i2c_set_clientdata(client, indio_dev); | |
166 | data->client = client; | |
167 | ||
168 | indio_dev->dev.parent = &client->dev; | |
2388eb1b | 169 | indio_dev->name = id->name; |
cf35ad61 PM |
170 | indio_dev->info = &mcp4725_info; |
171 | indio_dev->channels = &mcp4725_channel; | |
172 | indio_dev->num_channels = 1; | |
173 | indio_dev->modes = INDIO_DIRECT_MODE; | |
174 | ||
175 | data->vref_mv = platform_data->vref_mv; | |
176 | ||
177 | /* read current DAC value */ | |
178 | err = i2c_master_recv(client, inbuf, 3); | |
179 | if (err < 0) { | |
180 | dev_err(&client->dev, "failed to read DAC value"); | |
181 | goto exit_free_device; | |
182 | } | |
183 | data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4); | |
184 | ||
185 | err = iio_device_register(indio_dev); | |
186 | if (err) | |
187 | goto exit_free_device; | |
188 | ||
189 | dev_info(&client->dev, "MCP4725 DAC registered\n"); | |
190 | ||
191 | return 0; | |
192 | ||
193 | exit_free_device: | |
194 | iio_device_free(indio_dev); | |
195 | exit: | |
196 | return err; | |
197 | } | |
198 | ||
fc52692c | 199 | static int mcp4725_remove(struct i2c_client *client) |
cf35ad61 PM |
200 | { |
201 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | |
202 | ||
203 | iio_device_unregister(indio_dev); | |
204 | iio_device_free(indio_dev); | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
209 | static const struct i2c_device_id mcp4725_id[] = { | |
210 | { "mcp4725", 0 }, | |
211 | { } | |
212 | }; | |
213 | MODULE_DEVICE_TABLE(i2c, mcp4725_id); | |
214 | ||
215 | static struct i2c_driver mcp4725_driver = { | |
216 | .driver = { | |
217 | .name = MCP4725_DRV_NAME, | |
218 | .pm = MCP4725_PM_OPS, | |
219 | }, | |
220 | .probe = mcp4725_probe, | |
fc52692c | 221 | .remove = mcp4725_remove, |
cf35ad61 PM |
222 | .id_table = mcp4725_id, |
223 | }; | |
224 | module_i2c_driver(mcp4725_driver); | |
225 | ||
226 | MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); | |
227 | MODULE_DESCRIPTION("MCP4725 12-bit DAC"); | |
228 | MODULE_LICENSE("GPL"); |