1 // SPDX-License-Identifier: GPL-2.0-only
3 * ROHM 1780GLI Ambient Light Sensor Driver
5 * Copyright (C) 2016 Linaro Ltd.
7 * Loosely based on the previous BH1780 ALS misc driver
8 * Copyright (C) 2010 Texas Instruments
11 #include <linux/i2c.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/bitops.h>
22 #define BH1780_CMD_BIT BIT(7)
23 #define BH1780_REG_CONTROL 0x00
24 #define BH1780_REG_PARTID 0x0A
25 #define BH1780_REG_MANFID 0x0B
26 #define BH1780_REG_DLOW 0x0C
27 #define BH1780_REG_DHIGH 0x0D
29 #define BH1780_REVMASK GENMASK(3,0)
30 #define BH1780_POWMASK GENMASK(1,0)
31 #define BH1780_POFF (0x0)
32 #define BH1780_PON (0x3)
34 /* power on settling time in ms */
35 #define BH1780_PON_DELAY 2
36 /* max time before value available in ms */
37 #define BH1780_INTERVAL 250
40 struct i2c_client *client;
43 static int bh1780_write(struct bh1780_data *bh1780, u8 reg, u8 val)
45 int ret = i2c_smbus_write_byte_data(bh1780->client,
49 dev_err(&bh1780->client->dev,
50 "i2c_smbus_write_byte_data failed error "
51 "%d, register %01x\n",
56 static int bh1780_read(struct bh1780_data *bh1780, u8 reg)
58 int ret = i2c_smbus_read_byte_data(bh1780->client,
59 BH1780_CMD_BIT | reg);
61 dev_err(&bh1780->client->dev,
62 "i2c_smbus_read_byte_data failed error "
63 "%d, register %01x\n",
68 static int bh1780_read_word(struct bh1780_data *bh1780, u8 reg)
70 int ret = i2c_smbus_read_word_data(bh1780->client,
71 BH1780_CMD_BIT | reg);
73 dev_err(&bh1780->client->dev,
74 "i2c_smbus_read_word_data failed error "
75 "%d, register %01x\n",
80 static int bh1780_debugfs_reg_access(struct iio_dev *indio_dev,
81 unsigned int reg, unsigned int writeval,
82 unsigned int *readval)
84 struct bh1780_data *bh1780 = iio_priv(indio_dev);
88 return bh1780_write(bh1780, (u8)reg, (u8)writeval);
90 ret = bh1780_read(bh1780, (u8)reg);
99 static int bh1780_read_raw(struct iio_dev *indio_dev,
100 struct iio_chan_spec const *chan,
101 int *val, int *val2, long mask)
103 struct bh1780_data *bh1780 = iio_priv(indio_dev);
107 case IIO_CHAN_INFO_RAW:
108 switch (chan->type) {
110 pm_runtime_get_sync(&bh1780->client->dev);
111 value = bh1780_read_word(bh1780, BH1780_REG_DLOW);
114 pm_runtime_mark_last_busy(&bh1780->client->dev);
115 pm_runtime_put_autosuspend(&bh1780->client->dev);
122 case IIO_CHAN_INFO_INT_TIME:
124 *val2 = BH1780_INTERVAL * 1000;
125 return IIO_VAL_INT_PLUS_MICRO;
131 static const struct iio_info bh1780_info = {
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)
147 struct bh1780_data *bh1780;
148 struct i2c_adapter *adapter = client->adapter;
149 struct iio_dev *indio_dev;
151 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
154 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*bh1780));
158 bh1780 = iio_priv(indio_dev);
159 bh1780->client = client;
160 i2c_set_clientdata(client, indio_dev);
162 /* Power up the device */
163 ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
166 msleep(BH1780_PON_DELAY);
167 pm_runtime_get_noresume(&client->dev);
168 pm_runtime_set_active(&client->dev);
169 pm_runtime_enable(&client->dev);
171 ret = bh1780_read(bh1780, BH1780_REG_PARTID);
174 dev_info(&client->dev,
175 "Ambient Light Sensor, Rev : %lu\n",
176 (ret & BH1780_REVMASK));
179 * As the device takes 250 ms to even come up with a fresh
180 * measurement after power-on, do not shut it down unnecessarily.
181 * Set autosuspend to a five seconds.
183 pm_runtime_set_autosuspend_delay(&client->dev, 5000);
184 pm_runtime_use_autosuspend(&client->dev);
185 pm_runtime_put(&client->dev);
187 indio_dev->info = &bh1780_info;
188 indio_dev->name = "bh1780";
189 indio_dev->channels = bh1780_channels;
190 indio_dev->num_channels = ARRAY_SIZE(bh1780_channels);
191 indio_dev->modes = INDIO_DIRECT_MODE;
193 ret = iio_device_register(indio_dev);
199 pm_runtime_put_noidle(&client->dev);
200 pm_runtime_disable(&client->dev);
204 static void bh1780_remove(struct i2c_client *client)
206 struct iio_dev *indio_dev = i2c_get_clientdata(client);
207 struct bh1780_data *bh1780 = iio_priv(indio_dev);
210 iio_device_unregister(indio_dev);
211 pm_runtime_get_sync(&client->dev);
212 pm_runtime_put_noidle(&client->dev);
213 pm_runtime_disable(&client->dev);
214 ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
216 dev_err(&client->dev, "failed to power off (%pe)\n",
220 static int bh1780_runtime_suspend(struct device *dev)
222 struct i2c_client *client = to_i2c_client(dev);
223 struct iio_dev *indio_dev = i2c_get_clientdata(client);
224 struct bh1780_data *bh1780 = iio_priv(indio_dev);
227 ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
229 dev_err(dev, "failed to runtime suspend\n");
236 static int bh1780_runtime_resume(struct device *dev)
238 struct i2c_client *client = to_i2c_client(dev);
239 struct iio_dev *indio_dev = i2c_get_clientdata(client);
240 struct bh1780_data *bh1780 = iio_priv(indio_dev);
243 ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
245 dev_err(dev, "failed to runtime resume\n");
249 /* Wait for power on, then for a value to be available */
250 msleep(BH1780_PON_DELAY + BH1780_INTERVAL);
255 static DEFINE_RUNTIME_DEV_PM_OPS(bh1780_dev_pm_ops, bh1780_runtime_suspend,
256 bh1780_runtime_resume, NULL);
258 static const struct i2c_device_id bh1780_id[] = {
263 MODULE_DEVICE_TABLE(i2c, bh1780_id);
265 static const struct of_device_id of_bh1780_match[] = {
266 { .compatible = "rohm,bh1780gli", },
269 MODULE_DEVICE_TABLE(of, of_bh1780_match);
271 static struct i2c_driver bh1780_driver = {
272 .probe_new = bh1780_probe,
273 .remove = bh1780_remove,
274 .id_table = bh1780_id,
277 .pm = pm_ptr(&bh1780_dev_pm_ops),
278 .of_match_table = of_bh1780_match,
282 module_i2c_driver(bh1780_driver);
284 MODULE_DESCRIPTION("ROHM BH1780GLI Ambient Light Sensor Driver");
285 MODULE_LICENSE("GPL");