Blame view

drivers/misc/tsl2550.c 9.97 KB
74ba9207e   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
a92c344d8   Rodolfo Giometti   i2c: Add support ...
2
3
4
5
6
  /*
   *  tsl2550.c - Linux kernel modules for ambient light sensor
   *
   *  Copyright (C) 2007 Rodolfo Giometti <giometti@linux.it>
   *  Copyright (C) 2007 Eurotech S.p.A. <info@eurotech.it>
a92c344d8   Rodolfo Giometti   i2c: Add support ...
7
8
9
   */
  
  #include <linux/module.h>
a92c344d8   Rodolfo Giometti   i2c: Add support ...
10
11
12
  #include <linux/slab.h>
  #include <linux/i2c.h>
  #include <linux/mutex.h>
a92c344d8   Rodolfo Giometti   i2c: Add support ...
13
14
  
  #define TSL2550_DRV_NAME	"tsl2550"
ac7809414   Jean Delvare   i2c/tsl2550: Use ...
15
  #define DRIVER_VERSION		"1.2"
a92c344d8   Rodolfo Giometti   i2c: Add support ...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  
  /*
   * Defines
   */
  
  #define TSL2550_POWER_DOWN		0x00
  #define TSL2550_POWER_UP		0x03
  #define TSL2550_STANDARD_RANGE		0x18
  #define TSL2550_EXTENDED_RANGE		0x1d
  #define TSL2550_READ_ADC0		0x43
  #define TSL2550_READ_ADC1		0x83
  
  /*
   * Structs
   */
  
  struct tsl2550_data {
  	struct i2c_client *client;
  	struct mutex update_lock;
6a9bcced5   Jean Delvare   tsl2550: Move fro...
35
36
  	unsigned int power_state:1;
  	unsigned int operating_mode:1;
a92c344d8   Rodolfo Giometti   i2c: Add support ...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  };
  
  /*
   * Global data
   */
  
  static const u8 TSL2550_MODE_RANGE[2] = {
  	TSL2550_STANDARD_RANGE, TSL2550_EXTENDED_RANGE,
  };
  
  /*
   * Management functions
   */
  
  static int tsl2550_set_operating_mode(struct i2c_client *client, int mode)
  {
  	struct tsl2550_data *data = i2c_get_clientdata(client);
  
  	int ret = i2c_smbus_write_byte(client, TSL2550_MODE_RANGE[mode]);
  
  	data->operating_mode = mode;
  
  	return ret;
  }
  
  static int tsl2550_set_power_state(struct i2c_client *client, int state)
  {
  	struct tsl2550_data *data = i2c_get_clientdata(client);
  	int ret;
  
  	if (state == 0)
  		ret = i2c_smbus_write_byte(client, TSL2550_POWER_DOWN);
  	else {
  		ret = i2c_smbus_write_byte(client, TSL2550_POWER_UP);
  
  		/* On power up we should reset operating mode also... */
  		tsl2550_set_operating_mode(client, data->operating_mode);
  	}
  
  	data->power_state = state;
  
  	return ret;
  }
  
  static int tsl2550_get_adc_value(struct i2c_client *client, u8 cmd)
  {
ac7809414   Jean Delvare   i2c/tsl2550: Use ...
83
  	int ret;
a92c344d8   Rodolfo Giometti   i2c: Add support ...
84

ac7809414   Jean Delvare   i2c/tsl2550: Use ...
85
86
87
  	ret = i2c_smbus_read_byte_data(client, cmd);
  	if (ret < 0)
  		return ret;
a92c344d8   Rodolfo Giometti   i2c: Add support ...
88
  	if (!(ret & 0x80))
ac7809414   Jean Delvare   i2c/tsl2550: Use ...
89
  		return -EAGAIN;
a92c344d8   Rodolfo Giometti   i2c: Add support ...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  	return ret & 0x7f;	/* remove the "valid" bit */
  }
  
  /*
   * LUX calculation
   */
  
  #define	TSL2550_MAX_LUX		1846
  
  static const u8 ratio_lut[] = {
  	100, 100, 100, 100, 100, 100, 100, 100,
  	100, 100, 100, 100, 100, 100, 99, 99,
  	99, 99, 99, 99, 99, 99, 99, 99,
  	99, 99, 99, 98, 98, 98, 98, 98,
  	98, 98, 97, 97, 97, 97, 97, 96,
  	96, 96, 96, 95, 95, 95, 94, 94,
  	93, 93, 93, 92, 92, 91, 91, 90,
  	89, 89, 88, 87, 87, 86, 85, 84,
  	83, 82, 81, 80, 79, 78, 77, 75,
  	74, 73, 71, 69, 68, 66, 64, 62,
  	60, 58, 56, 54, 52, 49, 47, 44,
  	42, 41, 40, 40, 39, 39, 38, 38,
  	37, 37, 37, 36, 36, 36, 35, 35,
  	35, 35, 34, 34, 34, 34, 33, 33,
  	33, 33, 32, 32, 32, 32, 32, 31,
  	31, 31, 31, 31, 30, 30, 30, 30,
  	30,
  };
  
  static const u16 count_lut[] = {
  	0, 1, 2, 3, 4, 5, 6, 7,
  	8, 9, 10, 11, 12, 13, 14, 15,
  	16, 18, 20, 22, 24, 26, 28, 30,
  	32, 34, 36, 38, 40, 42, 44, 46,
  	49, 53, 57, 61, 65, 69, 73, 77,
  	81, 85, 89, 93, 97, 101, 105, 109,
  	115, 123, 131, 139, 147, 155, 163, 171,
  	179, 187, 195, 203, 211, 219, 227, 235,
  	247, 263, 279, 295, 311, 327, 343, 359,
  	375, 391, 407, 423, 439, 455, 471, 487,
  	511, 543, 575, 607, 639, 671, 703, 735,
  	767, 799, 831, 863, 895, 927, 959, 991,
  	1039, 1103, 1167, 1231, 1295, 1359, 1423, 1487,
  	1551, 1615, 1679, 1743, 1807, 1871, 1935, 1999,
  	2095, 2223, 2351, 2479, 2607, 2735, 2863, 2991,
  	3119, 3247, 3375, 3503, 3631, 3759, 3887, 4015,
  };
  
  /*
   * This function is described into Taos TSL2550 Designer's Notebook
   * pages 2, 3.
   */
  static int tsl2550_calculate_lux(u8 ch0, u8 ch1)
  {
  	unsigned int lux;
  
  	/* Look up count from channel values */
  	u16 c0 = count_lut[ch0];
  	u16 c1 = count_lut[ch1];
a92c344d8   Rodolfo Giometti   i2c: Add support ...
149
  	/* Avoid division by 0 and count 1 cannot be greater than count 0 */
96f699ad0   Michele Jr De Candia   i2c/tsl2550: Fix ...
150
151
  	if (c1 <= c0)
  		if (c0) {
f896ee51b   Colin Ian King   misc: tsl2550: re...
152
153
154
155
156
  			/*
  			 * Calculate ratio.
  			 * Note: the "128" is a scaling factor
  			 */
  			u8 r = c1 * 128 / c0;
96f699ad0   Michele Jr De Candia   i2c/tsl2550: Fix ...
157
158
159
160
161
  
  			/* Calculate LUX */
  			lux = ((c0 - c1) * ratio_lut[r]) / 256;
  		} else
  			lux = 0;
a92c344d8   Rodolfo Giometti   i2c: Add support ...
162
  	else
ce054546c   Matt Ranostay   tsl2550: fix lux1...
163
  		return 0;
a92c344d8   Rodolfo Giometti   i2c: Add support ...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  
  	/* LUX range check */
  	return lux > TSL2550_MAX_LUX ? TSL2550_MAX_LUX : lux;
  }
  
  /*
   * SysFS support
   */
  
  static ssize_t tsl2550_show_power_state(struct device *dev,
  		struct device_attribute *attr, char *buf)
  {
  	struct tsl2550_data *data = i2c_get_clientdata(to_i2c_client(dev));
  
  	return sprintf(buf, "%u
  ", data->power_state);
  }
  
  static ssize_t tsl2550_store_power_state(struct device *dev,
  		struct device_attribute *attr, const char *buf, size_t count)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct tsl2550_data *data = i2c_get_clientdata(client);
  	unsigned long val = simple_strtoul(buf, NULL, 10);
  	int ret;
ae8a35aef   Chen Gang   Drivers: Misc: ts...
189
  	if (val > 1)
a92c344d8   Rodolfo Giometti   i2c: Add support ...
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  		return -EINVAL;
  
  	mutex_lock(&data->update_lock);
  	ret = tsl2550_set_power_state(client, val);
  	mutex_unlock(&data->update_lock);
  
  	if (ret < 0)
  		return ret;
  
  	return count;
  }
  
  static DEVICE_ATTR(power_state, S_IWUSR | S_IRUGO,
  		   tsl2550_show_power_state, tsl2550_store_power_state);
  
  static ssize_t tsl2550_show_operating_mode(struct device *dev,
  		struct device_attribute *attr, char *buf)
  {
  	struct tsl2550_data *data = i2c_get_clientdata(to_i2c_client(dev));
  
  	return sprintf(buf, "%u
  ", data->operating_mode);
  }
  
  static ssize_t tsl2550_store_operating_mode(struct device *dev,
  		struct device_attribute *attr, const char *buf, size_t count)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct tsl2550_data *data = i2c_get_clientdata(client);
  	unsigned long val = simple_strtoul(buf, NULL, 10);
  	int ret;
ae8a35aef   Chen Gang   Drivers: Misc: ts...
221
  	if (val > 1)
a92c344d8   Rodolfo Giometti   i2c: Add support ...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  		return -EINVAL;
  
  	if (data->power_state == 0)
  		return -EBUSY;
  
  	mutex_lock(&data->update_lock);
  	ret = tsl2550_set_operating_mode(client, val);
  	mutex_unlock(&data->update_lock);
  
  	if (ret < 0)
  		return ret;
  
  	return count;
  }
  
  static DEVICE_ATTR(operating_mode, S_IWUSR | S_IRUGO,
  		   tsl2550_show_operating_mode, tsl2550_store_operating_mode);
  
  static ssize_t __tsl2550_show_lux(struct i2c_client *client, char *buf)
  {
5f5bfb09d   Michele Jr De Candia   i2c/tsl2550: Fix ...
242
  	struct tsl2550_data *data = i2c_get_clientdata(client);
a92c344d8   Rodolfo Giometti   i2c: Add support ...
243
244
245
246
247
248
249
  	u8 ch0, ch1;
  	int ret;
  
  	ret = tsl2550_get_adc_value(client, TSL2550_READ_ADC0);
  	if (ret < 0)
  		return ret;
  	ch0 = ret;
a92c344d8   Rodolfo Giometti   i2c: Add support ...
250
251
252
253
254
255
256
257
258
  	ret = tsl2550_get_adc_value(client, TSL2550_READ_ADC1);
  	if (ret < 0)
  		return ret;
  	ch1 = ret;
  
  	/* Do the job */
  	ret = tsl2550_calculate_lux(ch0, ch1);
  	if (ret < 0)
  		return ret;
5f5bfb09d   Michele Jr De Candia   i2c/tsl2550: Fix ...
259
260
  	if (data->operating_mode == 1)
  		ret *= 5;
a92c344d8   Rodolfo Giometti   i2c: Add support ...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
  
  	return sprintf(buf, "%d
  ", ret);
  }
  
  static ssize_t tsl2550_show_lux1_input(struct device *dev,
  			struct device_attribute *attr, char *buf)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct tsl2550_data *data = i2c_get_clientdata(client);
  	int ret;
  
  	/* No LUX data if not operational */
  	if (!data->power_state)
  		return -EBUSY;
  
  	mutex_lock(&data->update_lock);
  	ret = __tsl2550_show_lux(client, buf);
  	mutex_unlock(&data->update_lock);
  
  	return ret;
  }
  
  static DEVICE_ATTR(lux1_input, S_IRUGO,
  		   tsl2550_show_lux1_input, NULL);
  
  static struct attribute *tsl2550_attributes[] = {
  	&dev_attr_power_state.attr,
  	&dev_attr_operating_mode.attr,
  	&dev_attr_lux1_input.attr,
  	NULL
  };
  
  static const struct attribute_group tsl2550_attr_group = {
  	.attrs = tsl2550_attributes,
  };
  
  /*
   * Initialization function
   */
e296fb7f3   Jean Delvare   i2c/tsl2550: Spee...
301
  static int tsl2550_init_client(struct i2c_client *client)
a92c344d8   Rodolfo Giometti   i2c: Add support ...
302
303
  {
  	struct tsl2550_data *data = i2c_get_clientdata(client);
e296fb7f3   Jean Delvare   i2c/tsl2550: Spee...
304
  	int err;
a92c344d8   Rodolfo Giometti   i2c: Add support ...
305

e296fb7f3   Jean Delvare   i2c/tsl2550: Spee...
306
307
308
309
  	/*
  	 * Probe the chip. To do so we try to power up the device and then to
  	 * read back the 0x03 code
  	 */
ac7809414   Jean Delvare   i2c/tsl2550: Use ...
310
  	err = i2c_smbus_read_byte_data(client, TSL2550_POWER_UP);
e296fb7f3   Jean Delvare   i2c/tsl2550: Spee...
311
312
  	if (err < 0)
  		return err;
ac7809414   Jean Delvare   i2c/tsl2550: Use ...
313
  	if (err != TSL2550_POWER_UP)
e296fb7f3   Jean Delvare   i2c/tsl2550: Spee...
314
315
316
317
318
319
320
321
322
323
  		return -ENODEV;
  	data->power_state = 1;
  
  	/* Set the default operating mode */
  	err = i2c_smbus_write_byte(client,
  				   TSL2550_MODE_RANGE[data->operating_mode]);
  	if (err < 0)
  		return err;
  
  	return 0;
a92c344d8   Rodolfo Giometti   i2c: Add support ...
324
325
326
327
328
329
330
  }
  
  /*
   * I2C init/probing/exit functions
   */
  
  static struct i2c_driver tsl2550_driver;
80c8ae289   Bill Pemberton   misc: remove use ...
331
  static int tsl2550_probe(struct i2c_client *client,
d2653e927   Jean Delvare   i2c: Add support ...
332
  				   const struct i2c_device_id *id)
a92c344d8   Rodolfo Giometti   i2c: Add support ...
333
  {
3cc2decc6   Wolfram Sang   misc: tsl2550: si...
334
  	struct i2c_adapter *adapter = client->adapter;
a92c344d8   Rodolfo Giometti   i2c: Add support ...
335
336
  	struct tsl2550_data *data;
  	int *opmode, err = 0;
ac7809414   Jean Delvare   i2c/tsl2550: Use ...
337
338
  	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE
  					    | I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
a92c344d8   Rodolfo Giometti   i2c: Add support ...
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
  		err = -EIO;
  		goto exit;
  	}
  
  	data = kzalloc(sizeof(struct tsl2550_data), GFP_KERNEL);
  	if (!data) {
  		err = -ENOMEM;
  		goto exit;
  	}
  	data->client = client;
  	i2c_set_clientdata(client, data);
  
  	/* Check platform data */
  	opmode = client->dev.platform_data;
  	if (opmode) {
  		if (*opmode < 0 || *opmode > 1) {
  			dev_err(&client->dev, "invalid operating_mode (%d)
  ",
  					*opmode);
  			err = -EINVAL;
  			goto exit_kfree;
  		}
  		data->operating_mode = *opmode;
  	} else
  		data->operating_mode = 0;	/* default mode is standard */
  	dev_info(&client->dev, "%s operating mode
  ",
  			data->operating_mode ? "extended" : "standard");
a92c344d8   Rodolfo Giometti   i2c: Add support ...
367
368
369
  	mutex_init(&data->update_lock);
  
  	/* Initialize the TSL2550 chip */
e296fb7f3   Jean Delvare   i2c/tsl2550: Spee...
370
371
372
  	err = tsl2550_init_client(client);
  	if (err)
  		goto exit_kfree;
a92c344d8   Rodolfo Giometti   i2c: Add support ...
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
  
  	/* Register sysfs hooks */
  	err = sysfs_create_group(&client->dev.kobj, &tsl2550_attr_group);
  	if (err)
  		goto exit_kfree;
  
  	dev_info(&client->dev, "support ver. %s enabled
  ", DRIVER_VERSION);
  
  	return 0;
  
  exit_kfree:
  	kfree(data);
  exit:
  	return err;
  }
486a5c28c   Bill Pemberton   misc: remove use ...
389
  static int tsl2550_remove(struct i2c_client *client)
a92c344d8   Rodolfo Giometti   i2c: Add support ...
390
391
392
393
394
395
396
397
398
399
  {
  	sysfs_remove_group(&client->dev.kobj, &tsl2550_attr_group);
  
  	/* Power down the device */
  	tsl2550_set_power_state(client, 0);
  
  	kfree(i2c_get_clientdata(client));
  
  	return 0;
  }
a77610276   Lars-Peter Clausen   misc: tsl2550: Us...
400
  #ifdef CONFIG_PM_SLEEP
1b3e5baa8   Rodolfo Giometti   i2c/tsl2550: Add ...
401

a77610276   Lars-Peter Clausen   misc: tsl2550: Us...
402
  static int tsl2550_suspend(struct device *dev)
1b3e5baa8   Rodolfo Giometti   i2c/tsl2550: Add ...
403
  {
a77610276   Lars-Peter Clausen   misc: tsl2550: Us...
404
  	return tsl2550_set_power_state(to_i2c_client(dev), 0);
1b3e5baa8   Rodolfo Giometti   i2c/tsl2550: Add ...
405
  }
a77610276   Lars-Peter Clausen   misc: tsl2550: Us...
406
  static int tsl2550_resume(struct device *dev)
1b3e5baa8   Rodolfo Giometti   i2c/tsl2550: Add ...
407
  {
a77610276   Lars-Peter Clausen   misc: tsl2550: Us...
408
  	return tsl2550_set_power_state(to_i2c_client(dev), 1);
1b3e5baa8   Rodolfo Giometti   i2c/tsl2550: Add ...
409
  }
a77610276   Lars-Peter Clausen   misc: tsl2550: Us...
410
411
  static SIMPLE_DEV_PM_OPS(tsl2550_pm_ops, tsl2550_suspend, tsl2550_resume);
  #define TSL2550_PM_OPS (&tsl2550_pm_ops)
1b3e5baa8   Rodolfo Giometti   i2c/tsl2550: Add ...
412
  #else
a77610276   Lars-Peter Clausen   misc: tsl2550: Us...
413
  #define TSL2550_PM_OPS NULL
1b3e5baa8   Rodolfo Giometti   i2c/tsl2550: Add ...
414

a77610276   Lars-Peter Clausen   misc: tsl2550: Us...
415
  #endif /* CONFIG_PM_SLEEP */
1b3e5baa8   Rodolfo Giometti   i2c/tsl2550: Add ...
416

3760f7367   Jean Delvare   i2c: Convert most...
417
418
419
420
421
  static const struct i2c_device_id tsl2550_id[] = {
  	{ "tsl2550", 0 },
  	{ }
  };
  MODULE_DEVICE_TABLE(i2c, tsl2550_id);
d2ce8d6fe   Javier Martinez Canillas   misc: tsl2550: Ad...
422
423
424
425
426
  static const struct of_device_id tsl2550_of_match[] = {
  	{ .compatible = "taos,tsl2550" },
  	{ }
  };
  MODULE_DEVICE_TABLE(of, tsl2550_of_match);
a92c344d8   Rodolfo Giometti   i2c: Add support ...
427
428
429
  static struct i2c_driver tsl2550_driver = {
  	.driver = {
  		.name	= TSL2550_DRV_NAME,
d2ce8d6fe   Javier Martinez Canillas   misc: tsl2550: Ad...
430
  		.of_match_table = tsl2550_of_match,
a77610276   Lars-Peter Clausen   misc: tsl2550: Us...
431
  		.pm	= TSL2550_PM_OPS,
a92c344d8   Rodolfo Giometti   i2c: Add support ...
432
433
  	},
  	.probe	= tsl2550_probe,
2d6bed9ca   Bill Pemberton   drivers/misc: rem...
434
  	.remove	= tsl2550_remove,
3760f7367   Jean Delvare   i2c: Convert most...
435
  	.id_table = tsl2550_id,
a92c344d8   Rodolfo Giometti   i2c: Add support ...
436
  };
a64fe2ed7   Axel Lin   MISC: convert dri...
437
  module_i2c_driver(tsl2550_driver);
a92c344d8   Rodolfo Giometti   i2c: Add support ...
438
439
440
441
  
  MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  MODULE_DESCRIPTION("TSL2550 ambient light sensor driver");
  MODULE_LICENSE("GPL");
e296fb7f3   Jean Delvare   i2c/tsl2550: Spee...
442
  MODULE_VERSION(DRIVER_VERSION);