2 * ROHM 1780GLI Ambient Light Sensor Driver
4 * Copyright (C) 2016 Linaro Ltd.
6 * Loosely based on the previous BH1780 ALS misc driver
7 * Copyright (C) 2010 Texas Instruments
10 #include <linux/i2c.h>
11 #include <linux/slab.h>
12 #include <linux/platform_device.h>
13 #include <linux/delay.h>
14 #include <linux/module.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/bitops.h>
21 #define BH1780_CMD_BIT BIT(7)
22 #define BH1780_REG_CONTROL 0x00
23 #define BH1780_REG_PARTID 0x0A
24 #define BH1780_REG_MANFID 0x0B
25 #define BH1780_REG_DLOW 0x0C
26 #define BH1780_REG_DHIGH 0x0D
28 #define BH1780_REVMASK GENMASK(3,0)
29 #define BH1780_POWMASK GENMASK(1,0)
30 #define BH1780_POFF (0x0)
31 #define BH1780_PON (0x3)
33 /* power on settling time in ms */
34 #define BH1780_PON_DELAY 2
35 /* max time before value available in ms */
36 #define BH1780_INTERVAL 250
39 struct i2c_client *client;
42 static int bh1780_write(struct bh1780_data *bh1780, u8 reg, u8 val)
44 int ret = i2c_smbus_write_byte_data(bh1780->client,
48 dev_err(&bh1780->client->dev,
49 "i2c_smbus_write_byte_data failed error "
50 "%d, register %01x\n",
55 static int bh1780_read(struct bh1780_data *bh1780, u8 reg)
57 int ret = i2c_smbus_read_byte_data(bh1780->client,
58 BH1780_CMD_BIT | reg);
60 dev_err(&bh1780->client->dev,
61 "i2c_smbus_read_byte_data failed error "
62 "%d, register %01x\n",
67 static int bh1780_read_word(struct bh1780_data *bh1780, u8 reg)
69 int ret = i2c_smbus_read_word_data(bh1780->client,
70 BH1780_CMD_BIT | reg);
72 dev_err(&bh1780->client->dev,
73 "i2c_smbus_read_word_data failed error "
74 "%d, register %01x\n",
79 static int bh1780_debugfs_reg_access(struct iio_dev *indio_dev,
80 unsigned int reg, unsigned int writeval,
81 unsigned int *readval)
83 struct bh1780_data *bh1780 = iio_priv(indio_dev);
87 return bh1780_write(bh1780, (u8)reg, (u8)writeval);
89 ret = bh1780_read(bh1780, (u8)reg);
98 static int bh1780_read_raw(struct iio_dev *indio_dev,
99 struct iio_chan_spec const *chan,
100 int *val, int *val2, long mask)
102 struct bh1780_data *bh1780 = iio_priv(indio_dev);
106 case IIO_CHAN_INFO_RAW:
107 switch (chan->type) {
109 pm_runtime_get_sync(&bh1780->client->dev);
110 value = bh1780_read_word(bh1780, BH1780_REG_DLOW);
113 pm_runtime_mark_last_busy(&bh1780->client->dev);
114 pm_runtime_put_autosuspend(&bh1780->client->dev);
121 case IIO_CHAN_INFO_INT_TIME:
123 *val2 = BH1780_INTERVAL * 1000;
124 return IIO_VAL_INT_PLUS_MICRO;
130 static const struct iio_info bh1780_info = {
131 .driver_module = THIS_MODULE,
132 .read_raw = bh1780_read_raw,
133 .debugfs_reg_access = bh1780_debugfs_reg_access,
136 static const struct iio_chan_spec bh1780_channels[] = {
139 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
140 BIT(IIO_CHAN_INFO_INT_TIME)
144 static int bh1780_probe(struct i2c_client *client,
145 const struct i2c_device_id *id)
148 struct bh1780_data *bh1780;
149 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
150 struct iio_dev *indio_dev;
152 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
155 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*bh1780));
159 bh1780 = iio_priv(indio_dev);
160 bh1780->client = client;
161 i2c_set_clientdata(client, indio_dev);
163 /* Power up the device */
164 ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
167 msleep(BH1780_PON_DELAY);
168 pm_runtime_get_noresume(&client->dev);
169 pm_runtime_set_active(&client->dev);
170 pm_runtime_enable(&client->dev);
172 ret = bh1780_read(bh1780, BH1780_REG_PARTID);
175 dev_info(&client->dev,
176 "Ambient Light Sensor, Rev : %lu\n",
177 (ret & BH1780_REVMASK));
180 * As the device takes 250 ms to even come up with a fresh
181 * measurement after power-on, do not shut it down unnecessarily.
182 * Set autosuspend to a five seconds.
184 pm_runtime_set_autosuspend_delay(&client->dev, 5000);
185 pm_runtime_use_autosuspend(&client->dev);
186 pm_runtime_put(&client->dev);
188 indio_dev->dev.parent = &client->dev;
189 indio_dev->info = &bh1780_info;
190 indio_dev->name = "bh1780";
191 indio_dev->channels = bh1780_channels;
192 indio_dev->num_channels = ARRAY_SIZE(bh1780_channels);
193 indio_dev->modes = INDIO_DIRECT_MODE;
195 ret = iio_device_register(indio_dev);
201 pm_runtime_put_noidle(&client->dev);
202 pm_runtime_disable(&client->dev);
206 static int bh1780_remove(struct i2c_client *client)
208 struct iio_dev *indio_dev = i2c_get_clientdata(client);
209 struct bh1780_data *bh1780 = iio_priv(indio_dev);
212 iio_device_unregister(indio_dev);
213 pm_runtime_get_sync(&client->dev);
214 pm_runtime_put_noidle(&client->dev);
215 pm_runtime_disable(&client->dev);
216 ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
218 dev_err(&client->dev, "failed to power off\n");
226 static int bh1780_runtime_suspend(struct device *dev)
228 struct i2c_client *client = to_i2c_client(dev);
229 struct iio_dev *indio_dev = i2c_get_clientdata(client);
230 struct bh1780_data *bh1780 = iio_priv(indio_dev);
233 ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
235 dev_err(dev, "failed to runtime suspend\n");
242 static int bh1780_runtime_resume(struct device *dev)
244 struct i2c_client *client = to_i2c_client(dev);
245 struct iio_dev *indio_dev = i2c_get_clientdata(client);
246 struct bh1780_data *bh1780 = iio_priv(indio_dev);
249 ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
251 dev_err(dev, "failed to runtime resume\n");
255 /* Wait for power on, then for a value to be available */
256 msleep(BH1780_PON_DELAY + BH1780_INTERVAL);
260 #endif /* CONFIG_PM */
262 static const struct dev_pm_ops bh1780_dev_pm_ops = {
263 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
264 pm_runtime_force_resume)
265 SET_RUNTIME_PM_OPS(bh1780_runtime_suspend,
266 bh1780_runtime_resume, NULL)
269 static const struct i2c_device_id bh1780_id[] = {
274 MODULE_DEVICE_TABLE(i2c, bh1780_id);
277 static const struct of_device_id of_bh1780_match[] = {
278 { .compatible = "rohm,bh1780gli", },
281 MODULE_DEVICE_TABLE(of, of_bh1780_match);
284 static struct i2c_driver bh1780_driver = {
285 .probe = bh1780_probe,
286 .remove = bh1780_remove,
287 .id_table = bh1780_id,
290 .pm = &bh1780_dev_pm_ops,
291 .of_match_table = of_match_ptr(of_bh1780_match),
295 module_i2c_driver(bh1780_driver);
297 MODULE_DESCRIPTION("ROHM BH1780GLI Ambient Light Sensor Driver");
298 MODULE_LICENSE("GPL");