GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / iio / light / isl29125.c
1 /*
2  * isl29125.c - Support for Intersil ISL29125 RGB light sensor
3  *
4  * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
5  *
6  * This file is subject to the terms and conditions of version 2 of
7  * the GNU General Public License.  See the file COPYING in the main
8  * directory of this archive for more details.
9  *
10  * RGB light sensor with 16-bit channels for red, green, blue);
11  * 7-bit I2C slave address 0x44
12  *
13  * TODO: interrupt support, IR compensation, thresholds, 12bit
14  */
15
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/delay.h>
19 #include <linux/pm.h>
20
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/triggered_buffer.h>
26
27 #define ISL29125_DRV_NAME "isl29125"
28
29 #define ISL29125_DEVICE_ID 0x00
30 #define ISL29125_CONF1 0x01
31 #define ISL29125_CONF2 0x02
32 #define ISL29125_CONF3 0x03
33 #define ISL29125_STATUS 0x08
34 #define ISL29125_GREEN_DATA 0x09
35 #define ISL29125_RED_DATA 0x0b
36 #define ISL29125_BLUE_DATA 0x0d
37
38 #define ISL29125_ID 0x7d
39
40 #define ISL29125_MODE_MASK GENMASK(2, 0)
41 #define ISL29125_MODE_PD 0x0
42 #define ISL29125_MODE_G 0x1
43 #define ISL29125_MODE_R 0x2
44 #define ISL29125_MODE_B 0x3
45 #define ISL29125_MODE_RGB 0x5
46
47 #define ISL29125_SENSING_RANGE_0 5722   /* 375 lux full range */
48 #define ISL29125_SENSING_RANGE_1 152590 /* 10k lux full range */
49
50 #define ISL29125_MODE_RANGE BIT(3)
51
52 #define ISL29125_STATUS_CONV BIT(1)
53
54 struct isl29125_data {
55         struct i2c_client *client;
56         u8 conf1;
57         /* Ensure timestamp is naturally aligned */
58         struct {
59                 u16 chans[3];
60                 s64 timestamp __aligned(8);
61         } scan;
62 };
63
64 #define ISL29125_CHANNEL(_color, _si) { \
65         .type = IIO_INTENSITY, \
66         .modified = 1, \
67         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
68         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
69         .channel2 = IIO_MOD_LIGHT_##_color, \
70         .scan_index = _si, \
71         .scan_type = { \
72                 .sign = 'u', \
73                 .realbits = 16, \
74                 .storagebits = 16, \
75                 .endianness = IIO_CPU, \
76         }, \
77 }
78
79 static const struct iio_chan_spec isl29125_channels[] = {
80         ISL29125_CHANNEL(GREEN, 0),
81         ISL29125_CHANNEL(RED, 1),
82         ISL29125_CHANNEL(BLUE, 2),
83         IIO_CHAN_SOFT_TIMESTAMP(3),
84 };
85
86 static const struct {
87         u8 mode, data;
88 } isl29125_regs[] = {
89         {ISL29125_MODE_G, ISL29125_GREEN_DATA},
90         {ISL29125_MODE_R, ISL29125_RED_DATA},
91         {ISL29125_MODE_B, ISL29125_BLUE_DATA},
92 };
93
94 static int isl29125_read_data(struct isl29125_data *data, int si)
95 {
96         int tries = 5;
97         int ret;
98
99         ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
100                 data->conf1 | isl29125_regs[si].mode);
101         if (ret < 0)
102                 return ret;
103
104         msleep(101);
105
106         while (tries--) {
107                 ret = i2c_smbus_read_byte_data(data->client, ISL29125_STATUS);
108                 if (ret < 0)
109                         goto fail;
110                 if (ret & ISL29125_STATUS_CONV)
111                         break;
112                 msleep(20);
113         }
114
115         if (tries < 0) {
116                 dev_err(&data->client->dev, "data not ready\n");
117                 ret = -EIO;
118                 goto fail;
119         }
120
121         ret = i2c_smbus_read_word_data(data->client, isl29125_regs[si].data);
122
123 fail:
124         i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, data->conf1);
125         return ret;
126 }
127
128 static int isl29125_read_raw(struct iio_dev *indio_dev,
129                            struct iio_chan_spec const *chan,
130                            int *val, int *val2, long mask)
131 {
132         struct isl29125_data *data = iio_priv(indio_dev);
133         int ret;
134
135         switch (mask) {
136         case IIO_CHAN_INFO_RAW:
137                 ret = iio_device_claim_direct_mode(indio_dev);
138                 if (ret)
139                         return ret;
140                 ret = isl29125_read_data(data, chan->scan_index);
141                 iio_device_release_direct_mode(indio_dev);
142                 if (ret < 0)
143                         return ret;
144                 *val = ret;
145                 return IIO_VAL_INT;
146         case IIO_CHAN_INFO_SCALE:
147                 *val = 0;
148                 if (data->conf1 & ISL29125_MODE_RANGE)
149                         *val2 = ISL29125_SENSING_RANGE_1; /*10k lux full range*/
150                 else
151                         *val2 = ISL29125_SENSING_RANGE_0; /*375 lux full range*/
152                 return IIO_VAL_INT_PLUS_MICRO;
153         }
154         return -EINVAL;
155 }
156
157 static int isl29125_write_raw(struct iio_dev *indio_dev,
158                                struct iio_chan_spec const *chan,
159                                int val, int val2, long mask)
160 {
161         struct isl29125_data *data = iio_priv(indio_dev);
162
163         switch (mask) {
164         case IIO_CHAN_INFO_SCALE:
165                 if (val != 0)
166                         return -EINVAL;
167                 if (val2 == ISL29125_SENSING_RANGE_1)
168                         data->conf1 |= ISL29125_MODE_RANGE;
169                 else if (val2 == ISL29125_SENSING_RANGE_0)
170                         data->conf1 &= ~ISL29125_MODE_RANGE;
171                 else
172                         return -EINVAL;
173                 return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
174                         data->conf1);
175         default:
176                 return -EINVAL;
177         }
178 }
179
180 static irqreturn_t isl29125_trigger_handler(int irq, void *p)
181 {
182         struct iio_poll_func *pf = p;
183         struct iio_dev *indio_dev = pf->indio_dev;
184         struct isl29125_data *data = iio_priv(indio_dev);
185         int i, j = 0;
186
187         for_each_set_bit(i, indio_dev->active_scan_mask,
188                 indio_dev->masklength) {
189                 int ret = i2c_smbus_read_word_data(data->client,
190                         isl29125_regs[i].data);
191                 if (ret < 0)
192                         goto done;
193
194                 data->scan.chans[j++] = ret;
195         }
196
197         iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
198                 iio_get_time_ns(indio_dev));
199
200 done:
201         iio_trigger_notify_done(indio_dev->trig);
202
203         return IRQ_HANDLED;
204 }
205
206 static IIO_CONST_ATTR(scale_available, "0.005722 0.152590");
207
208 static struct attribute *isl29125_attributes[] = {
209         &iio_const_attr_scale_available.dev_attr.attr,
210         NULL
211 };
212
213 static const struct attribute_group isl29125_attribute_group = {
214         .attrs = isl29125_attributes,
215 };
216
217 static const struct iio_info isl29125_info = {
218         .read_raw = isl29125_read_raw,
219         .write_raw = isl29125_write_raw,
220         .attrs = &isl29125_attribute_group,
221         .driver_module = THIS_MODULE,
222 };
223
224 static int isl29125_buffer_preenable(struct iio_dev *indio_dev)
225 {
226         struct isl29125_data *data = iio_priv(indio_dev);
227
228         data->conf1 |= ISL29125_MODE_RGB;
229         return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
230                 data->conf1);
231 }
232
233 static int isl29125_buffer_predisable(struct iio_dev *indio_dev)
234 {
235         struct isl29125_data *data = iio_priv(indio_dev);
236         int ret;
237
238         ret = iio_triggered_buffer_predisable(indio_dev);
239         if (ret < 0)
240                 return ret;
241
242         data->conf1 &= ~ISL29125_MODE_MASK;
243         data->conf1 |= ISL29125_MODE_PD;
244         return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
245                 data->conf1);
246 }
247
248 static const struct iio_buffer_setup_ops isl29125_buffer_setup_ops = {
249         .preenable = isl29125_buffer_preenable,
250         .postenable = &iio_triggered_buffer_postenable,
251         .predisable = isl29125_buffer_predisable,
252 };
253
254 static int isl29125_probe(struct i2c_client *client,
255                            const struct i2c_device_id *id)
256 {
257         struct isl29125_data *data;
258         struct iio_dev *indio_dev;
259         int ret;
260
261         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
262         if (indio_dev == NULL)
263                 return -ENOMEM;
264
265         data = iio_priv(indio_dev);
266         i2c_set_clientdata(client, indio_dev);
267         data->client = client;
268
269         indio_dev->dev.parent = &client->dev;
270         indio_dev->info = &isl29125_info;
271         indio_dev->name = ISL29125_DRV_NAME;
272         indio_dev->channels = isl29125_channels;
273         indio_dev->num_channels = ARRAY_SIZE(isl29125_channels);
274         indio_dev->modes = INDIO_DIRECT_MODE;
275
276         ret = i2c_smbus_read_byte_data(data->client, ISL29125_DEVICE_ID);
277         if (ret < 0)
278                 return ret;
279         if (ret != ISL29125_ID)
280                 return -ENODEV;
281
282         data->conf1 = ISL29125_MODE_PD | ISL29125_MODE_RANGE;
283         ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
284                 data->conf1);
285         if (ret < 0)
286                 return ret;
287
288         ret = i2c_smbus_write_byte_data(data->client, ISL29125_STATUS, 0);
289         if (ret < 0)
290                 return ret;
291
292         ret = iio_triggered_buffer_setup(indio_dev, NULL,
293                 isl29125_trigger_handler, &isl29125_buffer_setup_ops);
294         if (ret < 0)
295                 return ret;
296
297         ret = iio_device_register(indio_dev);
298         if (ret < 0)
299                 goto buffer_cleanup;
300
301         return 0;
302
303 buffer_cleanup:
304         iio_triggered_buffer_cleanup(indio_dev);
305         return ret;
306 }
307
308 static int isl29125_powerdown(struct isl29125_data *data)
309 {
310         return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
311                 (data->conf1 & ~ISL29125_MODE_MASK) | ISL29125_MODE_PD);
312 }
313
314 static int isl29125_remove(struct i2c_client *client)
315 {
316         struct iio_dev *indio_dev = i2c_get_clientdata(client);
317
318         iio_device_unregister(indio_dev);
319         iio_triggered_buffer_cleanup(indio_dev);
320         isl29125_powerdown(iio_priv(indio_dev));
321
322         return 0;
323 }
324
325 #ifdef CONFIG_PM_SLEEP
326 static int isl29125_suspend(struct device *dev)
327 {
328         struct isl29125_data *data = iio_priv(i2c_get_clientdata(
329                 to_i2c_client(dev)));
330         return isl29125_powerdown(data);
331 }
332
333 static int isl29125_resume(struct device *dev)
334 {
335         struct isl29125_data *data = iio_priv(i2c_get_clientdata(
336                 to_i2c_client(dev)));
337         return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
338                 data->conf1);
339 }
340 #endif
341
342 static SIMPLE_DEV_PM_OPS(isl29125_pm_ops, isl29125_suspend, isl29125_resume);
343
344 static const struct i2c_device_id isl29125_id[] = {
345         { "isl29125", 0 },
346         { }
347 };
348 MODULE_DEVICE_TABLE(i2c, isl29125_id);
349
350 static struct i2c_driver isl29125_driver = {
351         .driver = {
352                 .name   = ISL29125_DRV_NAME,
353                 .pm     = &isl29125_pm_ops,
354         },
355         .probe          = isl29125_probe,
356         .remove         = isl29125_remove,
357         .id_table       = isl29125_id,
358 };
359 module_i2c_driver(isl29125_driver);
360
361 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
362 MODULE_DESCRIPTION("ISL29125 RGB light sensor driver");
363 MODULE_LICENSE("GPL");