summaryrefslogtreecommitdiff
path: root/drivers/staging/iio/light/cm3218.c
blob: 0ccf88038b78ec7b4a50c13a631146da06c32b10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
83
84
85
86
87
88
89
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
189
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/* drivers/input/misc/cm3218.c - cm3218 Ambient Light Sensor driver
 *
 * Copyright (c) 2012-2013, NVIDIA CORPORATION. All Rights Reserved.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/regmap.h>
#include <linux/delay.h>
#include <linux/pm_runtime.h>
#include <linux/notifier.h>
#include <linux/regulator/consumer.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>


#define CM3218_REG_CONFIGURE		0x00
#define CM3218_REG_ALS_DATA		0x04
#define CM3218_REG_MAX			0x04


#define CONFIGURE_ALS_MASK		0x01
#define CONFIGURE_ALS_EN		0x00

#define CONFIGURE_SHDN_MASK		CONFIGURE_ALS_MASK
#define CONFIGURE_SHDN_EN		0x01

#define LS_POLLING_DELAY	1000 /* mSec */
#define I2C_MAX_TIMEOUT		msecs_to_jiffies(20) /* 20 mSec */

enum {
	CHIP_POWER_OFF,
	CHIP_POWER_ON_ALS_OFF,
	CHIP_POWER_ON_ALS_ON,
};

struct cm3218_chip {
	struct i2c_client		*client;
	struct i2c_device_id		*id;
	struct mutex			lock;
	struct regulator_bulk_data	*consumers;
	struct notifier_block		regulator_nb;
	wait_queue_head_t		i2c_wait_queue;
	int				i2c_xfer_ready;
	struct regmap			*regmap;

	u8				als_state;
	bool is_als_on_before_suspend;
	int shutdown_complete;
};

/* regulators used by the device */
static struct regulator_bulk_data cm3218_consumers[] = {
	{
		.supply = "vdd",
	},
};

/* device's regmap configuration for i2c communication */
/* non cacheable registers*/
bool cm3218_volatile_reg(struct device *dev, unsigned int reg)
{
	return reg == CM3218_REG_ALS_DATA;
}

static const struct reg_default cm3218_reg_defaults = {
	.reg = 0x00,
	.def = 0x0400,
};

/* TODO * in linux-next we have to add
 * val_format_endian to take care of endianness
 * use regmap_access_table instead of volatile_reg call backs
 */
static const struct regmap_config cm3218_regmap_config = {
	.reg_bits = 8,
	.val_bits = 16,
	.volatile_reg = &cm3218_volatile_reg,
	.max_register = CM3218_REG_MAX,
	.reg_defaults = &cm3218_reg_defaults,
	.num_reg_defaults = 1,
	.cache_type = REGCACHE_RBTREE,
};

/* device's read/write functionality and a helper */
static void change_endianness_16(int *val)
{
	u8 *buf = val;
	u8 temp = buf[0];
	buf[0] = buf[1];
	buf[1] = temp;
}

static int _cm3218_register_read(struct cm3218_chip *chip, int reg, int *val)
{
	int ret;
	int temp;

	if (!chip->regmap)
		return -ENODEV;

	ret = wait_event_timeout(chip->i2c_wait_queue,
					chip->i2c_xfer_ready, I2C_MAX_TIMEOUT);
	if (!ret) {
		dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
				"error_msg: device not ready for i2c xfer\n",
				chip->id->name, __func__, __LINE__);
		return -ETIMEDOUT;
	}

	mutex_lock(&chip->lock);
	ret = regmap_read(chip->regmap, reg, val);
	if (ret)
		dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
				"error_msg:regmap_read fails\n",
				chip->id->name, __func__, __LINE__);

	change_endianness_16(val);
/*
	temp = i2c_smbus_read_word_data(chip->client, CM3218_REG_ALS_DATA);
	dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
				"als_i2c_data = %d\n",
				chip->id->name, __func__, __LINE__, temp);
*/	mutex_unlock(&chip->lock);
	return ret;
}

static int _cm3218_register_write(struct cm3218_chip *chip, int reg, int mask,
				int val)
{
	int ret, value;

	if (!chip->regmap)
		return -ENODEV;

	ret = wait_event_timeout(chip->i2c_wait_queue,
					chip->i2c_xfer_ready, I2C_MAX_TIMEOUT);
	if (!ret) {
		dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
				"error_msg: device not ready for i2c xfer\n",
				chip->id->name, __func__, __LINE__);
		return -ETIMEDOUT;
	}

	mutex_lock(&chip->lock);
	change_endianness_16(&val);
	ret = regmap_update_bits(chip->regmap, reg, mask, val);
	if (ret)
		dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
				"error_msg:regmap_write fails\n",
				chip->id->name, __func__, __LINE__);
	mutex_unlock(&chip->lock);

	return ret;
}

/* device's registration with iio to facilitate user operations */
static ssize_t cm3218_chan_regulator_enable(struct iio_dev *indio_dev,
		uintptr_t private, struct iio_chan_spec const *chan, char *buf)
{
	u8 enable;
	int ret = 0;
	struct cm3218_chip *chip = iio_priv(indio_dev);

	if (kstrtou8(buf, 10, &enable))
		return -EINVAL;

	if ((enable != 0) && (enable != 1))
		return -EINVAL;

	if (chan->type != IIO_LIGHT)
		return -EINVAL;

	if (enable == (chip->als_state != CHIP_POWER_OFF))
		return 1;

	if (!chip->consumers)
		goto success;

	if (enable)
		ret = regulator_bulk_enable(1, chip->consumers);
	else
		ret = regulator_bulk_disable(1, chip->consumers);

	if (ret) {
		dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
				"error_msg:_cm3218_register_read fails\n",
				chip->id->name, __func__, __LINE__);
		goto fail;
	}

success:
	chip->als_state = enable;
fail:
	return ret ? ret : 1;
}

static ssize_t cm3218_chan_enable(struct iio_dev *indio_dev, uintptr_t private,
		struct iio_chan_spec const *chan, char *buf)
{
	u8 enable;
	int ret;
	struct cm3218_chip *chip = iio_priv(indio_dev);

	if (kstrtou8(buf, 10, &enable))
		return -EINVAL;

	if ((enable != 0) && (enable != 1))
		return -EINVAL;

	if (chip->als_state == CHIP_POWER_OFF) {
		dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
				"error_msg:please enable regulator first\n",
				chip->id->name, __func__, __LINE__);
		return -EINVAL;
	}

	if (!((enable && (chip->als_state == CHIP_POWER_ON_ALS_OFF)) ||
		(!enable && (chip->als_state == CHIP_POWER_ON_ALS_ON))))
		return -EINVAL;

	if (chan->type != IIO_LIGHT)
		return -EINVAL;

	/* a small optimization*/
	if (enable == (chip->als_state - 1))
		goto success;

	if (enable) {
		ret = _cm3218_register_write(chip, CM3218_REG_CONFIGURE,
						CONFIGURE_ALS_MASK,
						CONFIGURE_ALS_EN);
		if (ret)
			return ret;
	} else {
		ret = _cm3218_register_write(chip, CM3218_REG_CONFIGURE,
						CONFIGURE_ALS_MASK,
						!CONFIGURE_ALS_EN);
		if (ret)
			return ret;
	}

success:
	/* a small optimization*/
	chip->als_state = enable + 1;
	return ret ? ret : 1;
}


/* chan_regulator_enable is used to enable regulators used by
 * particular channel.
 * chan_enable actually configures various registers to activate
 * a particular channel.
 */
static const struct iio_chan_spec_ext_info cm3218_ext_info[] = {
	{
		.name = "regulator_enable",
		.write = cm3218_chan_regulator_enable,
	}, {
		.name = "enable",
		.write = cm3218_chan_enable,
	},
	NULL,
};

static const struct iio_chan_spec cm3218_channels[] = {
	{
		.type = IIO_LIGHT,
		.ext_info = cm3218_ext_info,
	},
};

static int cm3218_read_raw(struct iio_dev *indio_dev,
	struct iio_chan_spec const *chan, int *val, int *val2, long mask)
{
	struct cm3218_chip *chip = iio_priv(indio_dev);
	int ret;
	int value;

	if (chip->als_state != CHIP_POWER_ON_ALS_ON)
		return -EINVAL;

	if (chan->type != IIO_LIGHT)
		return -EINVAL;

	ret = _cm3218_register_read(chip, CM3218_REG_ALS_DATA, &value);
	if (ret)
		dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
			"error_msg:_cm3218_register_read fails\n",
			chip->id->name, __func__, __LINE__);

	if (!ret) {
		*val = value;
		ret = IIO_VAL_INT;
	}
	return ret;
}

/* read_raw is used to report a channel's data to user
 * in non SI units
 */
static const struct iio_info cm3218_iio_info = {
	.driver_module = THIS_MODULE,
	.read_raw = &cm3218_read_raw,
};

/* chip's power management helpers */
static int cm3218_activate_standby_mode(struct cm3218_chip *chip)
{
	int ret;
	ret = _cm3218_register_write(chip, CM3218_REG_CONFIGURE,
					CONFIGURE_SHDN_MASK,
					CONFIGURE_SHDN_EN);
	return 0;
}

/* this detects the regulator enable/disable event and puts
 * the device to low power state if this device does not use the regulator */
static int cm3218_power_manager(struct notifier_block *regulator_nb,
				unsigned long event, void *v)
{
	struct cm3218_chip *chip;

	chip = container_of(regulator_nb, struct cm3218_chip, regulator_nb);

	if (event & (REGULATOR_EVENT_POST_ENABLE |
			REGULATOR_EVENT_OUT_POSTCHANGE)) {
		chip->i2c_xfer_ready = 1;
		cm3218_activate_standby_mode(chip);
	} else if (event & (REGULATOR_EVENT_DISABLE |
			REGULATOR_EVENT_FORCE_DISABLE)) {
		chip->i2c_xfer_ready = 0;
	}
	return NOTIFY_OK;
}

#ifdef CONFIG_PM_SLEEP
static int cm3218_suspend(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct cm3218_chip *chip = iio_priv(indio_dev);
	int ret = 0;

	if (!chip->consumers)
		return 0;


	/* assumes all other devices stop using this regulator */
	if (chip->als_state != CHIP_POWER_OFF)
		ret = regulator_bulk_disable(1, chip->consumers);

	if (ret) {
		dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
				"error_msg:regulator_bulk_disable fails" \
				"\ncm3218_suspend fails\n",
				chip->id->name, __func__, __LINE__);
		return ret;
	}

	if (!ret && regulator_is_enabled(chip->consumers[0].consumer))
		ret = cm3218_activate_standby_mode(chip);
	if (ret)
		dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
				"error_msg:cm3218_activate_standby_mode fails" \
				"\ncm3218_suspend fails\n",
				chip->id->name, __func__, __LINE__);
	return ret;
}

static int cm3218_resume(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct cm3218_chip *chip = iio_priv(indio_dev);
	int ret = 0;
	int temp;

	if (chip->als_state == CHIP_POWER_OFF)
		return 0;

	ret = regulator_bulk_enable(1, cm3218_consumers);
	if (ret) {
		dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
				"error_msg:regulator_bulk_enable fails" \
				"\ncm3218_suspend fails\n",
				chip->id->name, __func__, __LINE__);
		return ret;
	}

	mutex_lock(&chip->lock);
	if (chip->als_state == CHIP_POWER_ON_ALS_ON)
		ret = _cm3218_register_write(chip, CM3218_REG_CONFIGURE,
						CONFIGURE_ALS_MASK,
						CONFIGURE_ALS_EN);
	if (ret) {
		dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
				"error_msg:_cm3218_register_write fails" \
				"\ncm3218_suspend fails\n",
				chip->id->name, __func__, __LINE__);
		/* fall back to CHIP_POWER_OFF, we don't need partial resume */
		do {
			temp = regulator_bulk_disable(1, cm3218_consumers);
		} while (temp);
	}
	mutex_unlock(&chip->lock);
	return ret;
}

static SIMPLE_DEV_PM_OPS(cm3218_pm_ops, cm3218_suspend, cm3218_resume);
#define CM3218_PM_OPS (&cm3218_pm_ops)
#else
#define CM3218_PM_OPS NULL
#endif


/* device's i2c registration */
static int cm3218_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	int ret = 0;
	struct cm3218_chip *chip;
	struct iio_dev *indio_dev;
	struct regmap *regmap;
	u8 *buf;

	indio_dev = iio_device_alloc(sizeof(*chip));
	if (indio_dev == NULL) {
		dev_err(&client->dev, "idname:%s func:%s line:%d " \
			"error_msg:iio_device_alloc fails\n",
			id->name, __func__, __LINE__);
		return -ENOMEM;
	}
	chip = iio_priv(indio_dev);

	i2c_set_clientdata(client, indio_dev);
	chip->client = client;
	mutex_init(&chip->lock);

	regmap = devm_regmap_init_i2c(client, &cm3218_regmap_config);
	if (IS_ERR_OR_NULL(regmap)) {
		dev_err(&client->dev, "idname:%s func:%s line:%d " \
		"error_msg:devm_regmap_init_i2c fails\n",
		id->name, __func__, __LINE__);
		return -ENOMEM;
	}
	chip->regmap = regmap;

	ret = devm_regulator_bulk_get(&client->dev,
					ARRAY_SIZE(cm3218_consumers),
					cm3218_consumers);
	if (ret)
		dev_err(&client->dev, "idname:%s func:%s line:%d " \
			"error_msg:regulator_get fails\n",
			id->name, __func__, __LINE__);
	else
		chip->consumers = cm3218_consumers;

	if (chip->consumers) {
		chip->regulator_nb.notifier_call = cm3218_power_manager;
		ret = regulator_register_notifier(chip->consumers[0].consumer,
						&chip->regulator_nb);
		if (ret)
			dev_err(&client->dev, "idname:%s func:%s line:%d " \
				"error_msg:regulator_register_notifier fails\n",
				id->name, __func__, __LINE__);
	}

	indio_dev->info = &cm3218_iio_info;
	indio_dev->channels = cm3218_channels;
	indio_dev->num_channels = 1;
	indio_dev->name = id->name;
	indio_dev->dev.parent = &client->dev;
	indio_dev->modes = INDIO_DIRECT_MODE;
	ret = iio_device_register(indio_dev);
	if (ret) {
		dev_err(&client->dev, "idname:%s func:%s line:%d " \
			"error_msg:iio_device_register fails\n",
			id->name, __func__, __LINE__);
		goto err_iio_free;
	}

	init_waitqueue_head(&chip->i2c_wait_queue);
	chip->als_state = 0;
	if (regulator_is_enabled(chip->consumers[0].consumer)) {
		chip->i2c_xfer_ready = 1;
		cm3218_activate_standby_mode(chip);
	}

	dev_info(&client->dev, "idname:%s func:%s line:%d " \
			"probe success\n",
			id->name, __func__, __LINE__);

	return 0;

err_iio_free:
	mutex_destroy(&chip->lock);
	iio_device_free(indio_dev);
	return ret;
}

static void cm3218_shutdown(struct i2c_client *client)
{
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct cm3218_chip *chip = iio_priv(indio_dev);
	mutex_lock(&chip->lock);
	chip->shutdown_complete = 1;
	mutex_unlock(&chip->lock);
}

static int __devexit cm3218_remove(struct i2c_client *client)
{
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct cm3218_chip *chip = iio_priv(indio_dev);

	mutex_destroy(&chip->lock);
	iio_device_unregister(indio_dev);
	iio_device_free(indio_dev);
	return 0;
}

static const struct i2c_device_id cm3218_id[] = {
	{"cm3218", 0},
	{}
};
MODULE_DEVICE_TABLE(i2c, cm3218_id);

static const struct of_device_id cm3218_of_match[] = {
	{ .compatible = "capella,cm3218", },
	{ },
};
MODULE_DEVICE_TABLE(of, cm3218_of_match);

static struct i2c_driver cm3218_driver = {
	.class = I2C_CLASS_HWMON,
	.driver = {
		.name = "cm3218",
		.owner = THIS_MODULE,
		.of_match_table = cm3218_of_match,
		.pm = CM3218_PM_OPS,
	},
	.id_table = cm3218_id,
	.probe = cm3218_probe,
	.remove = __devexit_p(cm3218_remove),
	.shutdown = cm3218_shutdown,
};

static int __init cm3218_init(void)
{
	return i2c_add_driver(&cm3218_driver);
}

static void __exit cm3218_exit(void)
{
	i2c_del_driver(&cm3218_driver);
}

module_init(cm3218_init);
module_exit(cm3218_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("CM3218 Driver");
MODULE_AUTHOR("Sri Krishna chowdary <schowdary@nvidia.com>");