136edc939SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2aab9ba7eSPeter Meerwald /*
3aab9ba7eSPeter Meerwald * tsl4531.c - Support for TAOS TSL4531 ambient light sensor
4aab9ba7eSPeter Meerwald *
5aab9ba7eSPeter Meerwald * Copyright 2013 Peter Meerwald <pmeerw@pmeerw.net>
6aab9ba7eSPeter Meerwald *
7aab9ba7eSPeter Meerwald * IIO driver for the TSL4531x family
8aab9ba7eSPeter Meerwald * TSL45311/TSL45313: 7-bit I2C slave address 0x39
9aab9ba7eSPeter Meerwald * TSL45315/TSL45317: 7-bit I2C slave address 0x29
10aab9ba7eSPeter Meerwald *
11aab9ba7eSPeter Meerwald * TODO: single cycle measurement
12aab9ba7eSPeter Meerwald */
13aab9ba7eSPeter Meerwald
14aab9ba7eSPeter Meerwald #include <linux/module.h>
15aab9ba7eSPeter Meerwald #include <linux/i2c.h>
16aab9ba7eSPeter Meerwald #include <linux/err.h>
17aab9ba7eSPeter Meerwald #include <linux/delay.h>
18aab9ba7eSPeter Meerwald
19aab9ba7eSPeter Meerwald #include <linux/iio/iio.h>
20aab9ba7eSPeter Meerwald #include <linux/iio/sysfs.h>
21aab9ba7eSPeter Meerwald
22aab9ba7eSPeter Meerwald #define TSL4531_DRV_NAME "tsl4531"
23aab9ba7eSPeter Meerwald
246b57573bSPeter Meerwald #define TSL4531_COMMAND BIT(7)
25aab9ba7eSPeter Meerwald
266b57573bSPeter Meerwald #define TSL4531_CONTROL (TSL4531_COMMAND | 0x00)
276b57573bSPeter Meerwald #define TSL4531_CONFIG (TSL4531_COMMAND | 0x01)
286b57573bSPeter Meerwald #define TSL4531_DATA (TSL4531_COMMAND | 0x04)
296b57573bSPeter Meerwald #define TSL4531_ID (TSL4531_COMMAND | 0x0a)
30aab9ba7eSPeter Meerwald
31aab9ba7eSPeter Meerwald /* operating modes in control register */
32aab9ba7eSPeter Meerwald #define TSL4531_MODE_POWERDOWN 0x00
33aab9ba7eSPeter Meerwald #define TSL4531_MODE_SINGLE_ADC 0x02
34aab9ba7eSPeter Meerwald #define TSL4531_MODE_NORMAL 0x03
35aab9ba7eSPeter Meerwald
36aab9ba7eSPeter Meerwald /* integration time control in config register */
37aab9ba7eSPeter Meerwald #define TSL4531_TCNTRL_400MS 0x00
38aab9ba7eSPeter Meerwald #define TSL4531_TCNTRL_200MS 0x01
39aab9ba7eSPeter Meerwald #define TSL4531_TCNTRL_100MS 0x02
40aab9ba7eSPeter Meerwald
41aab9ba7eSPeter Meerwald /* part number in id register */
42aab9ba7eSPeter Meerwald #define TSL45311_ID 0x8
43aab9ba7eSPeter Meerwald #define TSL45313_ID 0x9
44aab9ba7eSPeter Meerwald #define TSL45315_ID 0xa
45aab9ba7eSPeter Meerwald #define TSL45317_ID 0xb
46aab9ba7eSPeter Meerwald #define TSL4531_ID_SHIFT 4
47aab9ba7eSPeter Meerwald
48aab9ba7eSPeter Meerwald struct tsl4531_data {
49aab9ba7eSPeter Meerwald struct i2c_client *client;
50aab9ba7eSPeter Meerwald struct mutex lock;
51aab9ba7eSPeter Meerwald int int_time;
52aab9ba7eSPeter Meerwald };
53aab9ba7eSPeter Meerwald
54aab9ba7eSPeter Meerwald static IIO_CONST_ATTR_INT_TIME_AVAIL("0.1 0.2 0.4");
55aab9ba7eSPeter Meerwald
56aab9ba7eSPeter Meerwald static struct attribute *tsl4531_attributes[] = {
57aab9ba7eSPeter Meerwald &iio_const_attr_integration_time_available.dev_attr.attr,
58aab9ba7eSPeter Meerwald NULL
59aab9ba7eSPeter Meerwald };
60aab9ba7eSPeter Meerwald
61aab9ba7eSPeter Meerwald static const struct attribute_group tsl4531_attribute_group = {
62aab9ba7eSPeter Meerwald .attrs = tsl4531_attributes,
63aab9ba7eSPeter Meerwald };
64aab9ba7eSPeter Meerwald
65aab9ba7eSPeter Meerwald static const struct iio_chan_spec tsl4531_channels[] = {
66aab9ba7eSPeter Meerwald {
67aab9ba7eSPeter Meerwald .type = IIO_LIGHT,
68aab9ba7eSPeter Meerwald .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
69aab9ba7eSPeter Meerwald BIT(IIO_CHAN_INFO_SCALE) |
70aab9ba7eSPeter Meerwald BIT(IIO_CHAN_INFO_INT_TIME)
71aab9ba7eSPeter Meerwald }
72aab9ba7eSPeter Meerwald };
73aab9ba7eSPeter Meerwald
tsl4531_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)74aab9ba7eSPeter Meerwald static int tsl4531_read_raw(struct iio_dev *indio_dev,
75aab9ba7eSPeter Meerwald struct iio_chan_spec const *chan,
76aab9ba7eSPeter Meerwald int *val, int *val2, long mask)
77aab9ba7eSPeter Meerwald {
78aab9ba7eSPeter Meerwald struct tsl4531_data *data = iio_priv(indio_dev);
79aab9ba7eSPeter Meerwald int ret;
80aab9ba7eSPeter Meerwald
81aab9ba7eSPeter Meerwald switch (mask) {
82aab9ba7eSPeter Meerwald case IIO_CHAN_INFO_RAW:
83aab9ba7eSPeter Meerwald ret = i2c_smbus_read_word_data(data->client,
84aab9ba7eSPeter Meerwald TSL4531_DATA);
85aab9ba7eSPeter Meerwald if (ret < 0)
86aab9ba7eSPeter Meerwald return ret;
87aab9ba7eSPeter Meerwald *val = ret;
88aab9ba7eSPeter Meerwald return IIO_VAL_INT;
89aab9ba7eSPeter Meerwald case IIO_CHAN_INFO_SCALE:
90aab9ba7eSPeter Meerwald /* 0.. 1x, 1 .. 2x, 2 .. 4x */
91aab9ba7eSPeter Meerwald *val = 1 << data->int_time;
92aab9ba7eSPeter Meerwald return IIO_VAL_INT;
93aab9ba7eSPeter Meerwald case IIO_CHAN_INFO_INT_TIME:
94aab9ba7eSPeter Meerwald if (data->int_time == 0)
95aab9ba7eSPeter Meerwald *val2 = 400000;
96aab9ba7eSPeter Meerwald else if (data->int_time == 1)
97aab9ba7eSPeter Meerwald *val2 = 200000;
98aab9ba7eSPeter Meerwald else if (data->int_time == 2)
99aab9ba7eSPeter Meerwald *val2 = 100000;
100aab9ba7eSPeter Meerwald else
101aab9ba7eSPeter Meerwald return -EINVAL;
102aab9ba7eSPeter Meerwald *val = 0;
103aab9ba7eSPeter Meerwald return IIO_VAL_INT_PLUS_MICRO;
104aab9ba7eSPeter Meerwald default:
105aab9ba7eSPeter Meerwald return -EINVAL;
106aab9ba7eSPeter Meerwald }
107aab9ba7eSPeter Meerwald }
108aab9ba7eSPeter Meerwald
tsl4531_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)109aab9ba7eSPeter Meerwald static int tsl4531_write_raw(struct iio_dev *indio_dev,
110aab9ba7eSPeter Meerwald struct iio_chan_spec const *chan,
111aab9ba7eSPeter Meerwald int val, int val2, long mask)
112aab9ba7eSPeter Meerwald {
113aab9ba7eSPeter Meerwald struct tsl4531_data *data = iio_priv(indio_dev);
114aab9ba7eSPeter Meerwald int int_time, ret;
115aab9ba7eSPeter Meerwald
116aab9ba7eSPeter Meerwald switch (mask) {
117aab9ba7eSPeter Meerwald case IIO_CHAN_INFO_INT_TIME:
118aab9ba7eSPeter Meerwald if (val != 0)
119aab9ba7eSPeter Meerwald return -EINVAL;
120aab9ba7eSPeter Meerwald if (val2 == 400000)
121aab9ba7eSPeter Meerwald int_time = 0;
122aab9ba7eSPeter Meerwald else if (val2 == 200000)
123aab9ba7eSPeter Meerwald int_time = 1;
124aab9ba7eSPeter Meerwald else if (val2 == 100000)
125aab9ba7eSPeter Meerwald int_time = 2;
126aab9ba7eSPeter Meerwald else
127aab9ba7eSPeter Meerwald return -EINVAL;
128aab9ba7eSPeter Meerwald mutex_lock(&data->lock);
129aab9ba7eSPeter Meerwald ret = i2c_smbus_write_byte_data(data->client,
130aab9ba7eSPeter Meerwald TSL4531_CONFIG, int_time);
131aab9ba7eSPeter Meerwald if (ret >= 0)
132aab9ba7eSPeter Meerwald data->int_time = int_time;
133aab9ba7eSPeter Meerwald mutex_unlock(&data->lock);
134aab9ba7eSPeter Meerwald return ret;
135aab9ba7eSPeter Meerwald default:
136aab9ba7eSPeter Meerwald return -EINVAL;
137aab9ba7eSPeter Meerwald }
138aab9ba7eSPeter Meerwald }
139aab9ba7eSPeter Meerwald
140aab9ba7eSPeter Meerwald static const struct iio_info tsl4531_info = {
141aab9ba7eSPeter Meerwald .read_raw = tsl4531_read_raw,
142aab9ba7eSPeter Meerwald .write_raw = tsl4531_write_raw,
143aab9ba7eSPeter Meerwald .attrs = &tsl4531_attribute_group,
144aab9ba7eSPeter Meerwald };
145aab9ba7eSPeter Meerwald
tsl4531_check_id(struct i2c_client * client)146aab9ba7eSPeter Meerwald static int tsl4531_check_id(struct i2c_client *client)
147aab9ba7eSPeter Meerwald {
148aab9ba7eSPeter Meerwald int ret = i2c_smbus_read_byte_data(client, TSL4531_ID);
149aab9ba7eSPeter Meerwald if (ret < 0)
150aab9ba7eSPeter Meerwald return ret;
151aab9ba7eSPeter Meerwald
152aab9ba7eSPeter Meerwald switch (ret >> TSL4531_ID_SHIFT) {
153aab9ba7eSPeter Meerwald case TSL45311_ID:
154aab9ba7eSPeter Meerwald case TSL45313_ID:
155aab9ba7eSPeter Meerwald case TSL45315_ID:
156aab9ba7eSPeter Meerwald case TSL45317_ID:
157aab9ba7eSPeter Meerwald return 0;
1587d87b3c5SDan Carpenter default:
1597d87b3c5SDan Carpenter return -ENODEV;
160aab9ba7eSPeter Meerwald }
161aab9ba7eSPeter Meerwald }
162aab9ba7eSPeter Meerwald
tsl4531_probe(struct i2c_client * client)163081f6b9dSUwe Kleine-König static int tsl4531_probe(struct i2c_client *client)
164aab9ba7eSPeter Meerwald {
165aab9ba7eSPeter Meerwald struct tsl4531_data *data;
166aab9ba7eSPeter Meerwald struct iio_dev *indio_dev;
167aab9ba7eSPeter Meerwald int ret;
168aab9ba7eSPeter Meerwald
169aab9ba7eSPeter Meerwald indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
170aab9ba7eSPeter Meerwald if (!indio_dev)
171aab9ba7eSPeter Meerwald return -ENOMEM;
172aab9ba7eSPeter Meerwald
173aab9ba7eSPeter Meerwald data = iio_priv(indio_dev);
174aab9ba7eSPeter Meerwald i2c_set_clientdata(client, indio_dev);
175aab9ba7eSPeter Meerwald data->client = client;
176aab9ba7eSPeter Meerwald mutex_init(&data->lock);
177aab9ba7eSPeter Meerwald
1787d87b3c5SDan Carpenter ret = tsl4531_check_id(client);
1797d87b3c5SDan Carpenter if (ret) {
180aab9ba7eSPeter Meerwald dev_err(&client->dev, "no TSL4531 sensor\n");
1817d87b3c5SDan Carpenter return ret;
182aab9ba7eSPeter Meerwald }
183aab9ba7eSPeter Meerwald
184aab9ba7eSPeter Meerwald ret = i2c_smbus_write_byte_data(data->client, TSL4531_CONTROL,
185aab9ba7eSPeter Meerwald TSL4531_MODE_NORMAL);
186aab9ba7eSPeter Meerwald if (ret < 0)
187aab9ba7eSPeter Meerwald return ret;
188aab9ba7eSPeter Meerwald
189aab9ba7eSPeter Meerwald ret = i2c_smbus_write_byte_data(data->client, TSL4531_CONFIG,
190aab9ba7eSPeter Meerwald TSL4531_TCNTRL_400MS);
191aab9ba7eSPeter Meerwald if (ret < 0)
192aab9ba7eSPeter Meerwald return ret;
193aab9ba7eSPeter Meerwald
194aab9ba7eSPeter Meerwald indio_dev->info = &tsl4531_info;
195aab9ba7eSPeter Meerwald indio_dev->channels = tsl4531_channels;
196aab9ba7eSPeter Meerwald indio_dev->num_channels = ARRAY_SIZE(tsl4531_channels);
197aab9ba7eSPeter Meerwald indio_dev->name = TSL4531_DRV_NAME;
198aab9ba7eSPeter Meerwald indio_dev->modes = INDIO_DIRECT_MODE;
199aab9ba7eSPeter Meerwald
200aab9ba7eSPeter Meerwald return iio_device_register(indio_dev);
201aab9ba7eSPeter Meerwald }
202aab9ba7eSPeter Meerwald
tsl4531_powerdown(struct i2c_client * client)203aab9ba7eSPeter Meerwald static int tsl4531_powerdown(struct i2c_client *client)
204aab9ba7eSPeter Meerwald {
205aab9ba7eSPeter Meerwald return i2c_smbus_write_byte_data(client, TSL4531_CONTROL,
206aab9ba7eSPeter Meerwald TSL4531_MODE_POWERDOWN);
207aab9ba7eSPeter Meerwald }
208aab9ba7eSPeter Meerwald
tsl4531_remove(struct i2c_client * client)209ed5c2f5fSUwe Kleine-König static void tsl4531_remove(struct i2c_client *client)
210aab9ba7eSPeter Meerwald {
211aab9ba7eSPeter Meerwald iio_device_unregister(i2c_get_clientdata(client));
212aab9ba7eSPeter Meerwald tsl4531_powerdown(client);
213aab9ba7eSPeter Meerwald }
214aab9ba7eSPeter Meerwald
tsl4531_suspend(struct device * dev)215aab9ba7eSPeter Meerwald static int tsl4531_suspend(struct device *dev)
216aab9ba7eSPeter Meerwald {
217aab9ba7eSPeter Meerwald return tsl4531_powerdown(to_i2c_client(dev));
218aab9ba7eSPeter Meerwald }
219aab9ba7eSPeter Meerwald
tsl4531_resume(struct device * dev)220aab9ba7eSPeter Meerwald static int tsl4531_resume(struct device *dev)
221aab9ba7eSPeter Meerwald {
222aab9ba7eSPeter Meerwald return i2c_smbus_write_byte_data(to_i2c_client(dev), TSL4531_CONTROL,
223aab9ba7eSPeter Meerwald TSL4531_MODE_NORMAL);
224aab9ba7eSPeter Meerwald }
225aab9ba7eSPeter Meerwald
2265ad7f3c9SJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(tsl4531_pm_ops, tsl4531_suspend,
2275ad7f3c9SJonathan Cameron tsl4531_resume);
228aab9ba7eSPeter Meerwald
229aab9ba7eSPeter Meerwald static const struct i2c_device_id tsl4531_id[] = {
230*4391affaSUwe Kleine-König { "tsl4531" },
231aab9ba7eSPeter Meerwald { }
232aab9ba7eSPeter Meerwald };
233aab9ba7eSPeter Meerwald MODULE_DEVICE_TABLE(i2c, tsl4531_id);
234aab9ba7eSPeter Meerwald
235aab9ba7eSPeter Meerwald static struct i2c_driver tsl4531_driver = {
236aab9ba7eSPeter Meerwald .driver = {
237aab9ba7eSPeter Meerwald .name = TSL4531_DRV_NAME,
2385ad7f3c9SJonathan Cameron .pm = pm_sleep_ptr(&tsl4531_pm_ops),
239aab9ba7eSPeter Meerwald },
2407cf15f42SUwe Kleine-König .probe = tsl4531_probe,
241aab9ba7eSPeter Meerwald .remove = tsl4531_remove,
242aab9ba7eSPeter Meerwald .id_table = tsl4531_id,
243aab9ba7eSPeter Meerwald };
244aab9ba7eSPeter Meerwald
245aab9ba7eSPeter Meerwald module_i2c_driver(tsl4531_driver);
246aab9ba7eSPeter Meerwald
247aab9ba7eSPeter Meerwald MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
248aab9ba7eSPeter Meerwald MODULE_DESCRIPTION("TAOS TSL4531 ambient light sensors driver");
249aab9ba7eSPeter Meerwald MODULE_LICENSE("GPL");
250