Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * apds9802als.c - apds9802  ALS Driver
  3 *
  4 * Copyright (C) 2009 Intel Corp
  5 *
  6 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7 *
  8 * This program is free software; you can redistribute it and/or modify
  9 * it under the terms of the GNU General Public License as published by
 10 * the Free Software Foundation; version 2 of the License.
 11 *
 12 * This program is distributed in the hope that it will be useful, but
 13 * WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License along
 18 * with this program; if not, write to the Free Software Foundation, Inc.,
 19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 21 *
 22 */
 23
 24#include <linux/module.h>
 25#include <linux/init.h>
 26#include <linux/slab.h>
 27#include <linux/i2c.h>
 28#include <linux/err.h>
 29#include <linux/delay.h>
 30#include <linux/mutex.h>
 31#include <linux/sysfs.h>
 32#include <linux/pm_runtime.h>
 33
 34#define ALS_MIN_RANGE_VAL 1
 35#define ALS_MAX_RANGE_VAL 2
 36#define POWER_STA_ENABLE 1
 37#define POWER_STA_DISABLE 0
 38
 39#define DRIVER_NAME "apds9802als"
 40
 41struct als_data {
 42	struct mutex mutex;
 43};
 44
 45static ssize_t als_sensing_range_show(struct device *dev,
 46			struct device_attribute *attr,  char *buf)
 47{
 48	struct i2c_client *client = to_i2c_client(dev);
 49	int  val;
 50
 51	val = i2c_smbus_read_byte_data(client, 0x81);
 52	if (val < 0)
 53		return val;
 54	if (val & 1)
 55		return sprintf(buf, "4095\n");
 56	else
 57		return sprintf(buf, "65535\n");
 58}
 59
 60static int als_wait_for_data_ready(struct device *dev)
 61{
 62	struct i2c_client *client = to_i2c_client(dev);
 63	int ret;
 64	int retry = 10;
 65
 66	do {
 67		msleep(30);
 68		ret = i2c_smbus_read_byte_data(client, 0x86);
 69	} while (!(ret & 0x80) && retry--);
 70
 71	if (!retry) {
 72		dev_warn(dev, "timeout waiting for data ready\n");
 73		return -ETIMEDOUT;
 74	}
 75
 76	return 0;
 77}
 78
 79static ssize_t als_lux0_input_data_show(struct device *dev,
 80			struct device_attribute *attr, char *buf)
 81{
 82	struct i2c_client *client = to_i2c_client(dev);
 83	struct als_data *data = i2c_get_clientdata(client);
 84	int ret_val;
 85	int temp;
 86
 87	/* Protect against parallel reads */
 88	pm_runtime_get_sync(dev);
 89	mutex_lock(&data->mutex);
 90
 91	/* clear EOC interrupt status */
 92	i2c_smbus_write_byte(client, 0x40);
 93	/* start measurement */
 94	temp = i2c_smbus_read_byte_data(client, 0x81);
 95	i2c_smbus_write_byte_data(client, 0x81, temp | 0x08);
 96
 97	ret_val = als_wait_for_data_ready(dev);
 98	if (ret_val < 0)
 99		goto failed;
100
101	temp = i2c_smbus_read_byte_data(client, 0x8C); /* LSB data */
102	if (temp < 0) {
103		ret_val = temp;
104		goto failed;
105	}
106	ret_val = i2c_smbus_read_byte_data(client, 0x8D); /* MSB data */
107	if (ret_val < 0)
108		goto failed;
109
110	mutex_unlock(&data->mutex);
111	pm_runtime_put_sync(dev);
112
113	temp = (ret_val << 8) | temp;
114	return sprintf(buf, "%d\n", temp);
115failed:
116	mutex_unlock(&data->mutex);
117	pm_runtime_put_sync(dev);
118	return ret_val;
119}
120
121static ssize_t als_sensing_range_store(struct device *dev,
122		struct device_attribute *attr, const  char *buf, size_t count)
123{
124	struct i2c_client *client = to_i2c_client(dev);
125	struct als_data *data = i2c_get_clientdata(client);
126	int ret_val;
127	unsigned long val;
128
129	if (strict_strtoul(buf, 10, &val))
130		return -EINVAL;
 
131
132	if (val < 4096)
133		val = 1;
134	else if (val < 65536)
135		val = 2;
136	else
137		return -ERANGE;
138
139	pm_runtime_get_sync(dev);
140
141	/* Make sure nobody else reads/modifies/writes 0x81 while we
142	   are active */
143	mutex_lock(&data->mutex);
144
145	ret_val = i2c_smbus_read_byte_data(client, 0x81);
146	if (ret_val < 0)
147		goto fail;
148
149	/* Reset the bits before setting them */
150	ret_val = ret_val & 0xFA;
151
152	if (val == 1) /* Setting detection range up to 4k LUX */
153		ret_val = (ret_val | 0x01);
154	else /* Setting detection range up to 64k LUX*/
155		ret_val = (ret_val | 0x00);
156
157	ret_val = i2c_smbus_write_byte_data(client, 0x81, ret_val);
158
159	if (ret_val >= 0) {
160		/* All OK */
161		mutex_unlock(&data->mutex);
162		pm_runtime_put_sync(dev);
163		return count;
164	}
165fail:
166	mutex_unlock(&data->mutex);
167	pm_runtime_put_sync(dev);
168	return ret_val;
169}
170
171static int als_set_power_state(struct i2c_client *client, bool on_off)
172{
173	int ret_val;
174	struct als_data *data = i2c_get_clientdata(client);
175
176	mutex_lock(&data->mutex);
177	ret_val = i2c_smbus_read_byte_data(client, 0x80);
178	if (ret_val < 0)
179		goto fail;
180	if (on_off)
181		ret_val = ret_val | 0x01;
182	else
183		ret_val = ret_val & 0xFE;
184	ret_val = i2c_smbus_write_byte_data(client, 0x80, ret_val);
185fail:
186	mutex_unlock(&data->mutex);
187	return ret_val;
188}
189
190static DEVICE_ATTR(lux0_sensor_range, S_IRUGO | S_IWUSR,
191	als_sensing_range_show, als_sensing_range_store);
192static DEVICE_ATTR(lux0_input, S_IRUGO, als_lux0_input_data_show, NULL);
193
194static struct attribute *mid_att_als[] = {
195	&dev_attr_lux0_sensor_range.attr,
196	&dev_attr_lux0_input.attr,
197	NULL
198};
199
200static struct attribute_group m_als_gr = {
201	.name = "apds9802als",
202	.attrs = mid_att_als
203};
204
205static int als_set_default_config(struct i2c_client *client)
206{
207	int ret_val;
208	/* Write the command and then switch on */
209	ret_val = i2c_smbus_write_byte_data(client, 0x80, 0x01);
210	if (ret_val < 0) {
211		dev_err(&client->dev, "failed default switch on write\n");
212		return ret_val;
213	}
214	/* detection range: 1~64K Lux, maunal measurement */
215	ret_val = i2c_smbus_write_byte_data(client, 0x81, 0x08);
216	if (ret_val < 0)
217		dev_err(&client->dev, "failed default LUX on write\n");
218
219	/*  We always get 0 for the 1st measurement after system power on,
220	 *  so make sure it is finished before user asks for data.
221	 */
222	als_wait_for_data_ready(&client->dev);
223
224	return ret_val;
225}
226
227static int apds9802als_probe(struct i2c_client *client,
228			     const struct i2c_device_id *id)
229{
230	int res;
231	struct als_data *data;
232
233	data = kzalloc(sizeof(struct als_data), GFP_KERNEL);
234	if (data == NULL) {
235		dev_err(&client->dev, "Memory allocation failed\n");
236		return -ENOMEM;
237	}
238	i2c_set_clientdata(client, data);
239	res = sysfs_create_group(&client->dev.kobj, &m_als_gr);
240	if (res) {
241		dev_err(&client->dev, "device create file failed\n");
242		goto als_error1;
243	}
244	dev_info(&client->dev, "ALS chip found\n");
245	als_set_default_config(client);
246	mutex_init(&data->mutex);
247
248	pm_runtime_set_active(&client->dev);
249	pm_runtime_enable(&client->dev);
250
251	return res;
252als_error1:
253	kfree(data);
254	return res;
255}
256
257static int __devexit apds9802als_remove(struct i2c_client *client)
258{
259	struct als_data *data = i2c_get_clientdata(client);
260
261	pm_runtime_get_sync(&client->dev);
262
263	als_set_power_state(client, false);
264	sysfs_remove_group(&client->dev.kobj, &m_als_gr);
265
266	pm_runtime_disable(&client->dev);
267	pm_runtime_set_suspended(&client->dev);
268	pm_runtime_put_noidle(&client->dev);
269
270	kfree(data);
271	return 0;
272}
273
274#ifdef CONFIG_PM
275static int apds9802als_suspend(struct i2c_client *client, pm_message_t mesg)
276{
277	als_set_power_state(client, false);
278	return 0;
279}
280
281static int apds9802als_resume(struct i2c_client *client)
282{
283	als_set_default_config(client);
284	return 0;
285}
286
287static int apds9802als_runtime_suspend(struct device *dev)
288{
289	struct i2c_client *client = to_i2c_client(dev);
290
291	als_set_power_state(client, false);
292	return 0;
293}
294
295static int apds9802als_runtime_resume(struct device *dev)
296{
297	struct i2c_client *client = to_i2c_client(dev);
298
299	als_set_power_state(client, true);
300	return 0;
301}
302
303static const struct dev_pm_ops apds9802als_pm_ops = {
304	.runtime_suspend = apds9802als_runtime_suspend,
305	.runtime_resume = apds9802als_runtime_resume,
306};
307
308#define APDS9802ALS_PM_OPS (&apds9802als_pm_ops)
309
310#else	/* CONFIG_PM */
311#define apds9802als_suspend NULL
312#define apds9802als_resume NULL
313#define APDS9802ALS_PM_OPS NULL
314#endif	/* CONFIG_PM */
315
316static struct i2c_device_id apds9802als_id[] = {
317	{ DRIVER_NAME, 0 },
318	{ }
319};
320
321MODULE_DEVICE_TABLE(i2c, apds9802als_id);
322
323static struct i2c_driver apds9802als_driver = {
324	.driver = {
325		.name = DRIVER_NAME,
326		.pm = APDS9802ALS_PM_OPS,
327	},
328	.probe = apds9802als_probe,
329	.remove = __devexit_p(apds9802als_remove),
330	.suspend = apds9802als_suspend,
331	.resume = apds9802als_resume,
332	.id_table = apds9802als_id,
333};
334
335static int __init sensor_apds9802als_init(void)
336{
337	return i2c_add_driver(&apds9802als_driver);
338}
339
340static void  __exit sensor_apds9802als_exit(void)
341{
342	i2c_del_driver(&apds9802als_driver);
343}
344module_init(sensor_apds9802als_init);
345module_exit(sensor_apds9802als_exit);
346
347MODULE_AUTHOR("Anantha Narayanan <Anantha.Narayanan@intel.com");
348MODULE_DESCRIPTION("Avago apds9802als ALS Driver");
349MODULE_LICENSE("GPL v2");
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * apds9802als.c - apds9802  ALS Driver
  4 *
  5 * Copyright (C) 2009 Intel Corp
  6 *
  7 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8 *
 
 
 
 
 
 
 
 
 
 
 
 
  9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 10 */
 11
 12#include <linux/module.h>
 
 13#include <linux/slab.h>
 14#include <linux/i2c.h>
 15#include <linux/err.h>
 16#include <linux/delay.h>
 17#include <linux/mutex.h>
 18#include <linux/sysfs.h>
 19#include <linux/pm_runtime.h>
 20
 21#define ALS_MIN_RANGE_VAL 1
 22#define ALS_MAX_RANGE_VAL 2
 23#define POWER_STA_ENABLE 1
 24#define POWER_STA_DISABLE 0
 25
 26#define DRIVER_NAME "apds9802als"
 27
 28struct als_data {
 29	struct mutex mutex;
 30};
 31
 32static ssize_t als_sensing_range_show(struct device *dev,
 33			struct device_attribute *attr,  char *buf)
 34{
 35	struct i2c_client *client = to_i2c_client(dev);
 36	int  val;
 37
 38	val = i2c_smbus_read_byte_data(client, 0x81);
 39	if (val < 0)
 40		return val;
 41	if (val & 1)
 42		return sprintf(buf, "4095\n");
 43	else
 44		return sprintf(buf, "65535\n");
 45}
 46
 47static int als_wait_for_data_ready(struct device *dev)
 48{
 49	struct i2c_client *client = to_i2c_client(dev);
 50	int ret;
 51	int retry = 10;
 52
 53	do {
 54		msleep(30);
 55		ret = i2c_smbus_read_byte_data(client, 0x86);
 56	} while (!(ret & 0x80) && retry--);
 57
 58	if (retry < 0) {
 59		dev_warn(dev, "timeout waiting for data ready\n");
 60		return -ETIMEDOUT;
 61	}
 62
 63	return 0;
 64}
 65
 66static ssize_t als_lux0_input_data_show(struct device *dev,
 67			struct device_attribute *attr, char *buf)
 68{
 69	struct i2c_client *client = to_i2c_client(dev);
 70	struct als_data *data = i2c_get_clientdata(client);
 71	int ret_val;
 72	int temp;
 73
 74	/* Protect against parallel reads */
 75	pm_runtime_get_sync(dev);
 76	mutex_lock(&data->mutex);
 77
 78	/* clear EOC interrupt status */
 79	i2c_smbus_write_byte(client, 0x40);
 80	/* start measurement */
 81	temp = i2c_smbus_read_byte_data(client, 0x81);
 82	i2c_smbus_write_byte_data(client, 0x81, temp | 0x08);
 83
 84	ret_val = als_wait_for_data_ready(dev);
 85	if (ret_val < 0)
 86		goto failed;
 87
 88	temp = i2c_smbus_read_byte_data(client, 0x8C); /* LSB data */
 89	if (temp < 0) {
 90		ret_val = temp;
 91		goto failed;
 92	}
 93	ret_val = i2c_smbus_read_byte_data(client, 0x8D); /* MSB data */
 94	if (ret_val < 0)
 95		goto failed;
 96
 97	mutex_unlock(&data->mutex);
 98	pm_runtime_put_sync(dev);
 99
100	temp = (ret_val << 8) | temp;
101	return sprintf(buf, "%d\n", temp);
102failed:
103	mutex_unlock(&data->mutex);
104	pm_runtime_put_sync(dev);
105	return ret_val;
106}
107
108static ssize_t als_sensing_range_store(struct device *dev,
109		struct device_attribute *attr, const  char *buf, size_t count)
110{
111	struct i2c_client *client = to_i2c_client(dev);
112	struct als_data *data = i2c_get_clientdata(client);
113	int ret_val;
114	unsigned long val;
115
116	ret_val = kstrtoul(buf, 10, &val);
117	if (ret_val)
118		return ret_val;
119
120	if (val < 4096)
121		val = 1;
122	else if (val < 65536)
123		val = 2;
124	else
125		return -ERANGE;
126
127	pm_runtime_get_sync(dev);
128
129	/* Make sure nobody else reads/modifies/writes 0x81 while we
130	   are active */
131	mutex_lock(&data->mutex);
132
133	ret_val = i2c_smbus_read_byte_data(client, 0x81);
134	if (ret_val < 0)
135		goto fail;
136
137	/* Reset the bits before setting them */
138	ret_val = ret_val & 0xFA;
139
140	if (val == 1) /* Setting detection range up to 4k LUX */
141		ret_val = (ret_val | 0x01);
142	else /* Setting detection range up to 64k LUX*/
143		ret_val = (ret_val | 0x00);
144
145	ret_val = i2c_smbus_write_byte_data(client, 0x81, ret_val);
146
147	if (ret_val >= 0) {
148		/* All OK */
149		mutex_unlock(&data->mutex);
150		pm_runtime_put_sync(dev);
151		return count;
152	}
153fail:
154	mutex_unlock(&data->mutex);
155	pm_runtime_put_sync(dev);
156	return ret_val;
157}
158
159static int als_set_power_state(struct i2c_client *client, bool on_off)
160{
161	int ret_val;
162	struct als_data *data = i2c_get_clientdata(client);
163
164	mutex_lock(&data->mutex);
165	ret_val = i2c_smbus_read_byte_data(client, 0x80);
166	if (ret_val < 0)
167		goto fail;
168	if (on_off)
169		ret_val = ret_val | 0x01;
170	else
171		ret_val = ret_val & 0xFE;
172	ret_val = i2c_smbus_write_byte_data(client, 0x80, ret_val);
173fail:
174	mutex_unlock(&data->mutex);
175	return ret_val;
176}
177
178static DEVICE_ATTR(lux0_sensor_range, S_IRUGO | S_IWUSR,
179	als_sensing_range_show, als_sensing_range_store);
180static DEVICE_ATTR(lux0_input, S_IRUGO, als_lux0_input_data_show, NULL);
181
182static struct attribute *mid_att_als[] = {
183	&dev_attr_lux0_sensor_range.attr,
184	&dev_attr_lux0_input.attr,
185	NULL
186};
187
188static const struct attribute_group m_als_gr = {
189	.name = "apds9802als",
190	.attrs = mid_att_als
191};
192
193static int als_set_default_config(struct i2c_client *client)
194{
195	int ret_val;
196	/* Write the command and then switch on */
197	ret_val = i2c_smbus_write_byte_data(client, 0x80, 0x01);
198	if (ret_val < 0) {
199		dev_err(&client->dev, "failed default switch on write\n");
200		return ret_val;
201	}
202	/* detection range: 1~64K Lux, maunal measurement */
203	ret_val = i2c_smbus_write_byte_data(client, 0x81, 0x08);
204	if (ret_val < 0)
205		dev_err(&client->dev, "failed default LUX on write\n");
206
207	/*  We always get 0 for the 1st measurement after system power on,
208	 *  so make sure it is finished before user asks for data.
209	 */
210	als_wait_for_data_ready(&client->dev);
211
212	return ret_val;
213}
214
215static int apds9802als_probe(struct i2c_client *client)
 
216{
217	int res;
218	struct als_data *data;
219
220	data = kzalloc(sizeof(struct als_data), GFP_KERNEL);
221	if (data == NULL) {
222		dev_err(&client->dev, "Memory allocation failed\n");
223		return -ENOMEM;
224	}
225	i2c_set_clientdata(client, data);
226	res = sysfs_create_group(&client->dev.kobj, &m_als_gr);
227	if (res) {
228		dev_err(&client->dev, "device create file failed\n");
229		goto als_error1;
230	}
231	dev_info(&client->dev, "ALS chip found\n");
232	als_set_default_config(client);
233	mutex_init(&data->mutex);
234
235	pm_runtime_set_active(&client->dev);
236	pm_runtime_enable(&client->dev);
237
238	return res;
239als_error1:
240	kfree(data);
241	return res;
242}
243
244static void apds9802als_remove(struct i2c_client *client)
245{
246	struct als_data *data = i2c_get_clientdata(client);
247
248	pm_runtime_get_sync(&client->dev);
249
250	als_set_power_state(client, false);
251	sysfs_remove_group(&client->dev.kobj, &m_als_gr);
252
253	pm_runtime_disable(&client->dev);
254	pm_runtime_set_suspended(&client->dev);
255	pm_runtime_put_noidle(&client->dev);
256
257	kfree(data);
 
258}
259
260#ifdef CONFIG_PM
 
 
 
 
 
 
 
 
 
 
 
261
262static int apds9802als_suspend(struct device *dev)
263{
264	struct i2c_client *client = to_i2c_client(dev);
265
266	als_set_power_state(client, false);
267	return 0;
268}
269
270static int apds9802als_resume(struct device *dev)
271{
272	struct i2c_client *client = to_i2c_client(dev);
273
274	als_set_power_state(client, true);
275	return 0;
276}
277
278static UNIVERSAL_DEV_PM_OPS(apds9802als_pm_ops, apds9802als_suspend,
279	apds9802als_resume, NULL);
 
 
280
281#define APDS9802ALS_PM_OPS (&apds9802als_pm_ops)
282
283#else	/* CONFIG_PM */
 
 
284#define APDS9802ALS_PM_OPS NULL
285#endif	/* CONFIG_PM */
286
287static const struct i2c_device_id apds9802als_id[] = {
288	{ DRIVER_NAME, 0 },
289	{ }
290};
291
292MODULE_DEVICE_TABLE(i2c, apds9802als_id);
293
294static struct i2c_driver apds9802als_driver = {
295	.driver = {
296		.name = DRIVER_NAME,
297		.pm = APDS9802ALS_PM_OPS,
298	},
299	.probe = apds9802als_probe,
300	.remove = apds9802als_remove,
 
 
301	.id_table = apds9802als_id,
302};
303
304module_i2c_driver(apds9802als_driver);
 
 
 
 
 
 
 
 
 
 
305
306MODULE_AUTHOR("Anantha Narayanan <Anantha.Narayanan@intel.com");
307MODULE_DESCRIPTION("Avago apds9802als ALS Driver");
308MODULE_LICENSE("GPL v2");