xref: /linux/drivers/iio/light/bh1780.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ROHM 1780GLI Ambient Light Sensor Driver
4  *
5  * Copyright (C) 2016 Linaro Ltd.
6  * Author: Linus Walleij <linus.walleij@linaro.org>
7  * Loosely based on the previous BH1780 ALS misc driver
8  * Copyright (C) 2010 Texas Instruments
9  * Author: Hemanth V <hemanthv@ti.com>
10  */
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/pm_runtime.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/bitops.h>
20 
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
27 
28 #define BH1780_REVMASK		GENMASK(3,0)
29 #define BH1780_POWMASK		GENMASK(1,0)
30 #define BH1780_POFF		(0x0)
31 #define BH1780_PON		(0x3)
32 
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
37 
38 struct bh1780_data {
39 	struct i2c_client *client;
40 };
41 
bh1780_write(struct bh1780_data * bh1780,u8 reg,u8 val)42 static int bh1780_write(struct bh1780_data *bh1780, u8 reg, u8 val)
43 {
44 	int ret = i2c_smbus_write_byte_data(bh1780->client,
45 					    BH1780_CMD_BIT | reg,
46 					    val);
47 	if (ret < 0)
48 		dev_err(&bh1780->client->dev,
49 			"i2c_smbus_write_byte_data failed error "
50 			"%d, register %01x\n",
51 			ret, reg);
52 	return ret;
53 }
54 
bh1780_read(struct bh1780_data * bh1780,u8 reg)55 static int bh1780_read(struct bh1780_data *bh1780, u8 reg)
56 {
57 	int ret = i2c_smbus_read_byte_data(bh1780->client,
58 					   BH1780_CMD_BIT | reg);
59 	if (ret < 0)
60 		dev_err(&bh1780->client->dev,
61 			"i2c_smbus_read_byte_data failed error "
62 			"%d, register %01x\n",
63 			ret, reg);
64 	return ret;
65 }
66 
bh1780_read_word(struct bh1780_data * bh1780,u8 reg)67 static int bh1780_read_word(struct bh1780_data *bh1780, u8 reg)
68 {
69 	int ret = i2c_smbus_read_word_data(bh1780->client,
70 					   BH1780_CMD_BIT | reg);
71 	if (ret < 0)
72 		dev_err(&bh1780->client->dev,
73 			"i2c_smbus_read_word_data failed error "
74 			"%d, register %01x\n",
75 			ret, reg);
76 	return ret;
77 }
78 
bh1780_debugfs_reg_access(struct iio_dev * indio_dev,unsigned int reg,unsigned int writeval,unsigned int * readval)79 static int bh1780_debugfs_reg_access(struct iio_dev *indio_dev,
80 			      unsigned int reg, unsigned int writeval,
81 			      unsigned int *readval)
82 {
83 	struct bh1780_data *bh1780 = iio_priv(indio_dev);
84 	int ret;
85 
86 	if (!readval)
87 		return bh1780_write(bh1780, (u8)reg, (u8)writeval);
88 
89 	ret = bh1780_read(bh1780, (u8)reg);
90 	if (ret < 0)
91 		return ret;
92 
93 	*readval = ret;
94 
95 	return 0;
96 }
97 
bh1780_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)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)
101 {
102 	struct bh1780_data *bh1780 = iio_priv(indio_dev);
103 	int value;
104 
105 	switch (mask) {
106 	case IIO_CHAN_INFO_RAW:
107 		switch (chan->type) {
108 		case IIO_LIGHT:
109 			pm_runtime_get_sync(&bh1780->client->dev);
110 			value = bh1780_read_word(bh1780, BH1780_REG_DLOW);
111 			pm_runtime_put_autosuspend(&bh1780->client->dev);
112 			if (value < 0)
113 				return value;
114 			*val = value;
115 
116 			return IIO_VAL_INT;
117 		default:
118 			return -EINVAL;
119 		}
120 	case IIO_CHAN_INFO_INT_TIME:
121 		*val = 0;
122 		*val2 = BH1780_INTERVAL * 1000;
123 		return IIO_VAL_INT_PLUS_MICRO;
124 	default:
125 		return -EINVAL;
126 	}
127 }
128 
129 static const struct iio_info bh1780_info = {
130 	.read_raw = bh1780_read_raw,
131 	.debugfs_reg_access = bh1780_debugfs_reg_access,
132 };
133 
134 static const struct iio_chan_spec bh1780_channels[] = {
135 	{
136 		.type = IIO_LIGHT,
137 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
138 				      BIT(IIO_CHAN_INFO_INT_TIME)
139 	}
140 };
141 
bh1780_probe(struct i2c_client * client)142 static int bh1780_probe(struct i2c_client *client)
143 {
144 	int ret;
145 	struct bh1780_data *bh1780;
146 	struct i2c_adapter *adapter = client->adapter;
147 	struct iio_dev *indio_dev;
148 
149 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
150 		return -EIO;
151 
152 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*bh1780));
153 	if (!indio_dev)
154 		return -ENOMEM;
155 
156 	bh1780 = iio_priv(indio_dev);
157 	bh1780->client = client;
158 	i2c_set_clientdata(client, indio_dev);
159 
160 	/* Power up the device */
161 	ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
162 	if (ret < 0)
163 		return ret;
164 	msleep(BH1780_PON_DELAY);
165 	pm_runtime_get_noresume(&client->dev);
166 	pm_runtime_set_active(&client->dev);
167 	pm_runtime_enable(&client->dev);
168 
169 	ret = bh1780_read(bh1780, BH1780_REG_PARTID);
170 	if (ret < 0)
171 		goto out_disable_pm;
172 	dev_info(&client->dev,
173 		 "Ambient Light Sensor, Rev : %lu\n",
174 		 (ret & BH1780_REVMASK));
175 
176 	/*
177 	 * As the device takes 250 ms to even come up with a fresh
178 	 * measurement after power-on, do not shut it down unnecessarily.
179 	 * Set autosuspend to a five seconds.
180 	 */
181 	pm_runtime_set_autosuspend_delay(&client->dev, 5000);
182 	pm_runtime_use_autosuspend(&client->dev);
183 	pm_runtime_put(&client->dev);
184 
185 	indio_dev->info = &bh1780_info;
186 	indio_dev->name = "bh1780";
187 	indio_dev->channels = bh1780_channels;
188 	indio_dev->num_channels = ARRAY_SIZE(bh1780_channels);
189 	indio_dev->modes = INDIO_DIRECT_MODE;
190 
191 	ret = iio_device_register(indio_dev);
192 	if (ret)
193 		goto out_disable_pm;
194 	return 0;
195 
196 out_disable_pm:
197 	pm_runtime_put_noidle(&client->dev);
198 	pm_runtime_disable(&client->dev);
199 	return ret;
200 }
201 
bh1780_remove(struct i2c_client * client)202 static void bh1780_remove(struct i2c_client *client)
203 {
204 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
205 	struct bh1780_data *bh1780 = iio_priv(indio_dev);
206 	int ret;
207 
208 	iio_device_unregister(indio_dev);
209 	pm_runtime_get_sync(&client->dev);
210 	pm_runtime_put_noidle(&client->dev);
211 	pm_runtime_disable(&client->dev);
212 	ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
213 	if (ret < 0)
214 		dev_err(&client->dev, "failed to power off (%pe)\n",
215 			ERR_PTR(ret));
216 }
217 
bh1780_runtime_suspend(struct device * dev)218 static int bh1780_runtime_suspend(struct device *dev)
219 {
220 	struct i2c_client *client = to_i2c_client(dev);
221 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
222 	struct bh1780_data *bh1780 = iio_priv(indio_dev);
223 	int ret;
224 
225 	ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
226 	if (ret < 0) {
227 		dev_err(dev, "failed to runtime suspend\n");
228 		return ret;
229 	}
230 
231 	return 0;
232 }
233 
bh1780_runtime_resume(struct device * dev)234 static int bh1780_runtime_resume(struct device *dev)
235 {
236 	struct i2c_client *client = to_i2c_client(dev);
237 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
238 	struct bh1780_data *bh1780 = iio_priv(indio_dev);
239 	int ret;
240 
241 	ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
242 	if (ret < 0) {
243 		dev_err(dev, "failed to runtime resume\n");
244 		return ret;
245 	}
246 
247 	/* Wait for power on, then for a value to be available */
248 	msleep(BH1780_PON_DELAY + BH1780_INTERVAL);
249 
250 	return 0;
251 }
252 
253 static DEFINE_RUNTIME_DEV_PM_OPS(bh1780_dev_pm_ops, bh1780_runtime_suspend,
254 				bh1780_runtime_resume, NULL);
255 
256 static const struct i2c_device_id bh1780_id[] = {
257 	{ .name = "bh1780" },
258 	{ }
259 };
260 
261 MODULE_DEVICE_TABLE(i2c, bh1780_id);
262 
263 static const struct of_device_id of_bh1780_match[] = {
264 	{ .compatible = "rohm,bh1780gli", },
265 	{ }
266 };
267 MODULE_DEVICE_TABLE(of, of_bh1780_match);
268 
269 static struct i2c_driver bh1780_driver = {
270 	.probe		= bh1780_probe,
271 	.remove		= bh1780_remove,
272 	.id_table	= bh1780_id,
273 	.driver = {
274 		.name = "bh1780",
275 		.pm = pm_ptr(&bh1780_dev_pm_ops),
276 		.of_match_table = of_bh1780_match,
277 	},
278 };
279 
280 module_i2c_driver(bh1780_driver);
281 
282 MODULE_DESCRIPTION("ROHM BH1780GLI Ambient Light Sensor Driver");
283 MODULE_LICENSE("GPL");
284 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
285