1/*
2 * Copyright (C) 2011 Kionix, Inc.
3 * Written by Chris Hudson <chudson@kionix.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 * 02111-1307, USA
18 */
19
20#include <linux/delay.h>
21#include <linux/i2c.h>
22#include <linux/input.h>
23#include <linux/interrupt.h>
24#include <linux/module.h>
25#include <linux/slab.h>
26#include <linux/input/kxtj9.h>
27#include <linux/input-polldev.h>
28
29#define NAME			"kxtj9"
30#define G_MAX			8000
31/* OUTPUT REGISTERS */
32#define XOUT_L			0x06
33#define WHO_AM_I		0x0F
34/* CONTROL REGISTERS */
35#define INT_REL			0x1A
36#define CTRL_REG1		0x1B
37#define INT_CTRL1		0x1E
38#define DATA_CTRL		0x21
39/* CONTROL REGISTER 1 BITS */
40#define PC1_OFF			0x7F
41#define PC1_ON			(1 << 7)
42/* Data ready funtion enable bit: set during probe if using irq mode */
43#define DRDYE			(1 << 5)
44/* DATA CONTROL REGISTER BITS */
45#define ODR12_5F		0
46#define ODR25F			1
47#define ODR50F			2
48#define ODR100F		3
49#define ODR200F		4
50#define ODR400F		5
51#define ODR800F		6
52/* INTERRUPT CONTROL REGISTER 1 BITS */
53/* Set these during probe if using irq mode */
54#define KXTJ9_IEL		(1 << 3)
55#define KXTJ9_IEA		(1 << 4)
56#define KXTJ9_IEN		(1 << 5)
57/* INPUT_ABS CONSTANTS */
58#define FUZZ			3
59#define FLAT			3
60/* RESUME STATE INDICES */
61#define RES_DATA_CTRL		0
62#define RES_CTRL_REG1		1
63#define RES_INT_CTRL1		2
64#define RESUME_ENTRIES		3
65
66/*
67 * The following table lists the maximum appropriate poll interval for each
68 * available output data rate.
69 */
70static const struct {
71	unsigned int cutoff;
72	u8 mask;
73} kxtj9_odr_table[] = {
74	{ 3,	ODR800F },
75	{ 5,	ODR400F },
76	{ 10,	ODR200F },
77	{ 20,	ODR100F },
78	{ 40,	ODR50F  },
79	{ 80,	ODR25F  },
80	{ 0,	ODR12_5F},
81};
82
83struct kxtj9_data {
84	struct i2c_client *client;
85	struct kxtj9_platform_data pdata;
86	struct input_dev *input_dev;
87#ifdef CONFIG_INPUT_KXTJ9_POLLED_MODE
88	struct input_polled_dev *poll_dev;
89#endif
90	unsigned int last_poll_interval;
91	u8 shift;
92	u8 ctrl_reg1;
93	u8 data_ctrl;
94	u8 int_ctrl;
95};
96
97static int kxtj9_i2c_read(struct kxtj9_data *tj9, u8 addr, u8 *data, int len)
98{
99	struct i2c_msg msgs[] = {
100		{
101			.addr = tj9->client->addr,
102			.flags = tj9->client->flags,
103			.len = 1,
104			.buf = &addr,
105		},
106		{
107			.addr = tj9->client->addr,
108			.flags = tj9->client->flags | I2C_M_RD,
109			.len = len,
110			.buf = data,
111		},
112	};
113
114	return i2c_transfer(tj9->client->adapter, msgs, 2);
115}
116
117static void kxtj9_report_acceleration_data(struct kxtj9_data *tj9)
118{
119	s16 acc_data[3]; /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
120	s16 x, y, z;
121	int err;
122
123	err = kxtj9_i2c_read(tj9, XOUT_L, (u8 *)acc_data, 6);
124	if (err < 0)
125		dev_err(&tj9->client->dev, "accelerometer data read failed\n");
126
127	x = le16_to_cpu(acc_data[tj9->pdata.axis_map_x]);
128	y = le16_to_cpu(acc_data[tj9->pdata.axis_map_y]);
129	z = le16_to_cpu(acc_data[tj9->pdata.axis_map_z]);
130
131	x >>= tj9->shift;
132	y >>= tj9->shift;
133	z >>= tj9->shift;
134
135	input_report_abs(tj9->input_dev, ABS_X, tj9->pdata.negate_x ? -x : x);
136	input_report_abs(tj9->input_dev, ABS_Y, tj9->pdata.negate_y ? -y : y);
137	input_report_abs(tj9->input_dev, ABS_Z, tj9->pdata.negate_z ? -z : z);
138	input_sync(tj9->input_dev);
139}
140
141static irqreturn_t kxtj9_isr(int irq, void *dev)
142{
143	struct kxtj9_data *tj9 = dev;
144	int err;
145
146	/* data ready is the only possible interrupt type */
147	kxtj9_report_acceleration_data(tj9);
148
149	err = i2c_smbus_read_byte_data(tj9->client, INT_REL);
150	if (err < 0)
151		dev_err(&tj9->client->dev,
152			"error clearing interrupt status: %d\n", err);
153
154	return IRQ_HANDLED;
155}
156
157static int kxtj9_update_g_range(struct kxtj9_data *tj9, u8 new_g_range)
158{
159	switch (new_g_range) {
160	case KXTJ9_G_2G:
161		tj9->shift = 4;
162		break;
163	case KXTJ9_G_4G:
164		tj9->shift = 3;
165		break;
166	case KXTJ9_G_8G:
167		tj9->shift = 2;
168		break;
169	default:
170		return -EINVAL;
171	}
172
173	tj9->ctrl_reg1 &= 0xe7;
174	tj9->ctrl_reg1 |= new_g_range;
175
176	return 0;
177}
178
179static int kxtj9_update_odr(struct kxtj9_data *tj9, unsigned int poll_interval)
180{
181	int err;
182	int i;
183
184	/* Use the lowest ODR that can support the requested poll interval */
185	for (i = 0; i < ARRAY_SIZE(kxtj9_odr_table); i++) {
186		tj9->data_ctrl = kxtj9_odr_table[i].mask;
187		if (poll_interval < kxtj9_odr_table[i].cutoff)
188			break;
189	}
190
191	err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, 0);
192	if (err < 0)
193		return err;
194
195	err = i2c_smbus_write_byte_data(tj9->client, DATA_CTRL, tj9->data_ctrl);
196	if (err < 0)
197		return err;
198
199	err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1);
200	if (err < 0)
201		return err;
202
203	return 0;
204}
205
206static int kxtj9_device_power_on(struct kxtj9_data *tj9)
207{
208	if (tj9->pdata.power_on)
209		return tj9->pdata.power_on();
210
211	return 0;
212}
213
214static void kxtj9_device_power_off(struct kxtj9_data *tj9)
215{
216	int err;
217
218	tj9->ctrl_reg1 &= PC1_OFF;
219	err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1);
220	if (err < 0)
221		dev_err(&tj9->client->dev, "soft power off failed\n");
222
223	if (tj9->pdata.power_off)
224		tj9->pdata.power_off();
225}
226
227static int kxtj9_enable(struct kxtj9_data *tj9)
228{
229	int err;
230
231	err = kxtj9_device_power_on(tj9);
232	if (err < 0)
233		return err;
234
235	/* ensure that PC1 is cleared before updating control registers */
236	err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, 0);
237	if (err < 0)
238		return err;
239
240	/* only write INT_CTRL_REG1 if in irq mode */
241	if (tj9->client->irq) {
242		err = i2c_smbus_write_byte_data(tj9->client,
243						INT_CTRL1, tj9->int_ctrl);
244		if (err < 0)
245			return err;
246	}
247
248	err = kxtj9_update_g_range(tj9, tj9->pdata.g_range);
249	if (err < 0)
250		return err;
251
252	/* turn on outputs */
253	tj9->ctrl_reg1 |= PC1_ON;
254	err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1);
255	if (err < 0)
256		return err;
257
258	err = kxtj9_update_odr(tj9, tj9->last_poll_interval);
259	if (err < 0)
260		return err;
261
262	/* clear initial interrupt if in irq mode */
263	if (tj9->client->irq) {
264		err = i2c_smbus_read_byte_data(tj9->client, INT_REL);
265		if (err < 0) {
266			dev_err(&tj9->client->dev,
267				"error clearing interrupt: %d\n", err);
268			goto fail;
269		}
270	}
271
272	return 0;
273
274fail:
275	kxtj9_device_power_off(tj9);
276	return err;
277}
278
279static void kxtj9_disable(struct kxtj9_data *tj9)
280{
281	kxtj9_device_power_off(tj9);
282}
283
284static int kxtj9_input_open(struct input_dev *input)
285{
286	struct kxtj9_data *tj9 = input_get_drvdata(input);
287
288	return kxtj9_enable(tj9);
289}
290
291static void kxtj9_input_close(struct input_dev *dev)
292{
293	struct kxtj9_data *tj9 = input_get_drvdata(dev);
294
295	kxtj9_disable(tj9);
296}
297
298static void __devinit kxtj9_init_input_device(struct kxtj9_data *tj9,
299					      struct input_dev *input_dev)
300{
301	__set_bit(EV_ABS, input_dev->evbit);
302	input_set_abs_params(input_dev, ABS_X, -G_MAX, G_MAX, FUZZ, FLAT);
303	input_set_abs_params(input_dev, ABS_Y, -G_MAX, G_MAX, FUZZ, FLAT);
304	input_set_abs_params(input_dev, ABS_Z, -G_MAX, G_MAX, FUZZ, FLAT);
305
306	input_dev->name = "kxtj9_accel";
307	input_dev->id.bustype = BUS_I2C;
308	input_dev->dev.parent = &tj9->client->dev;
309}
310
311static int __devinit kxtj9_setup_input_device(struct kxtj9_data *tj9)
312{
313	struct input_dev *input_dev;
314	int err;
315
316	input_dev = input_allocate_device();
317	if (!input_dev) {
318		dev_err(&tj9->client->dev, "input device allocate failed\n");
319		return -ENOMEM;
320	}
321
322	tj9->input_dev = input_dev;
323
324	input_dev->open = kxtj9_input_open;
325	input_dev->close = kxtj9_input_close;
326	input_set_drvdata(input_dev, tj9);
327
328	kxtj9_init_input_device(tj9, input_dev);
329
330	err = input_register_device(tj9->input_dev);
331	if (err) {
332		dev_err(&tj9->client->dev,
333			"unable to register input polled device %s: %d\n",
334			tj9->input_dev->name, err);
335		input_free_device(tj9->input_dev);
336		return err;
337	}
338
339	return 0;
340}
341
342/*
343 * When IRQ mode is selected, we need to provide an interface to allow the user
344 * to change the output data rate of the part.  For consistency, we are using
345 * the set_poll method, which accepts a poll interval in milliseconds, and then
346 * calls update_odr() while passing this value as an argument.  In IRQ mode, the
347 * data outputs will not be read AT the requested poll interval, rather, the
348 * lowest ODR that can support the requested interval.  The client application
349 * will be responsible for retrieving data from the input node at the desired
350 * interval.
351 */
352
353/* Returns currently selected poll interval (in ms) */
354static ssize_t kxtj9_get_poll(struct device *dev,
355				struct device_attribute *attr, char *buf)
356{
357	struct i2c_client *client = to_i2c_client(dev);
358	struct kxtj9_data *tj9 = i2c_get_clientdata(client);
359
360	return sprintf(buf, "%d\n", tj9->last_poll_interval);
361}
362
363/* Allow users to select a new poll interval (in ms) */
364static ssize_t kxtj9_set_poll(struct device *dev, struct device_attribute *attr,
365						const char *buf, size_t count)
366{
367	struct i2c_client *client = to_i2c_client(dev);
368	struct kxtj9_data *tj9 = i2c_get_clientdata(client);
369	struct input_dev *input_dev = tj9->input_dev;
370	unsigned int interval;
371	int error;
372
373	error = kstrtouint(buf, 10, &interval);
374	if (error < 0)
375		return error;
376
377	/* Lock the device to prevent races with open/close (and itself) */
378	mutex_lock(&input_dev->mutex);
379
380	disable_irq(client->irq);
381
382	/*
383	 * Set current interval to the greater of the minimum interval or
384	 * the requested interval
385	 */
386	tj9->last_poll_interval = max(interval, tj9->pdata.min_interval);
387
388	kxtj9_update_odr(tj9, tj9->last_poll_interval);
389
390	enable_irq(client->irq);
391	mutex_unlock(&input_dev->mutex);
392
393	return count;
394}
395
396static DEVICE_ATTR(poll, S_IRUGO|S_IWUSR, kxtj9_get_poll, kxtj9_set_poll);
397
398static struct attribute *kxtj9_attributes[] = {
399	&dev_attr_poll.attr,
400	NULL
401};
402
403static struct attribute_group kxtj9_attribute_group = {
404	.attrs = kxtj9_attributes
405};
406
407
408#ifdef CONFIG_INPUT_KXTJ9_POLLED_MODE
409static void kxtj9_poll(struct input_polled_dev *dev)
410{
411	struct kxtj9_data *tj9 = dev->private;
412	unsigned int poll_interval = dev->poll_interval;
413
414	kxtj9_report_acceleration_data(tj9);
415
416	if (poll_interval != tj9->last_poll_interval) {
417		kxtj9_update_odr(tj9, poll_interval);
418		tj9->last_poll_interval = poll_interval;
419	}
420}
421
422static void kxtj9_polled_input_open(struct input_polled_dev *dev)
423{
424	struct kxtj9_data *tj9 = dev->private;
425
426	kxtj9_enable(tj9);
427}
428
429static void kxtj9_polled_input_close(struct input_polled_dev *dev)
430{
431	struct kxtj9_data *tj9 = dev->private;
432
433	kxtj9_disable(tj9);
434}
435
436static int __devinit kxtj9_setup_polled_device(struct kxtj9_data *tj9)
437{
438	int err;
439	struct input_polled_dev *poll_dev;
440	poll_dev = input_allocate_polled_device();
441
442	if (!poll_dev) {
443		dev_err(&tj9->client->dev,
444			"Failed to allocate polled device\n");
445		return -ENOMEM;
446	}
447
448	tj9->poll_dev = poll_dev;
449	tj9->input_dev = poll_dev->input;
450
451	poll_dev->private = tj9;
452	poll_dev->poll = kxtj9_poll;
453	poll_dev->open = kxtj9_polled_input_open;
454	poll_dev->close = kxtj9_polled_input_close;
455
456	kxtj9_init_input_device(tj9, poll_dev->input);
457
458	err = input_register_polled_device(poll_dev);
459	if (err) {
460		dev_err(&tj9->client->dev,
461			"Unable to register polled device, err=%d\n", err);
462		input_free_polled_device(poll_dev);
463		return err;
464	}
465
466	return 0;
467}
468
469static void __devexit kxtj9_teardown_polled_device(struct kxtj9_data *tj9)
470{
471	input_unregister_polled_device(tj9->poll_dev);
472	input_free_polled_device(tj9->poll_dev);
473}
474
475#else
476
477static inline int kxtj9_setup_polled_device(struct kxtj9_data *tj9)
478{
479	return -ENOSYS;
480}
481
482static inline void kxtj9_teardown_polled_device(struct kxtj9_data *tj9)
483{
484}
485
486#endif
487
488static int __devinit kxtj9_verify(struct kxtj9_data *tj9)
489{
490	int retval;
491
492	retval = kxtj9_device_power_on(tj9);
493	if (retval < 0)
494		return retval;
495
496	retval = i2c_smbus_read_byte_data(tj9->client, WHO_AM_I);
497	if (retval < 0) {
498		dev_err(&tj9->client->dev, "read err int source\n");
499		goto out;
500	}
501
502	retval = (retval != 0x07 && retval != 0x08) ? -EIO : 0;
503
504out:
505	kxtj9_device_power_off(tj9);
506	return retval;
507}
508
509static int __devinit kxtj9_probe(struct i2c_client *client,
510				 const struct i2c_device_id *id)
511{
512	const struct kxtj9_platform_data *pdata = client->dev.platform_data;
513	struct kxtj9_data *tj9;
514	int err;
515
516	if (!i2c_check_functionality(client->adapter,
517				I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE_DATA)) {
518		dev_err(&client->dev, "client is not i2c capable\n");
519		return -ENXIO;
520	}
521
522	if (!pdata) {
523		dev_err(&client->dev, "platform data is NULL; exiting\n");
524		return -EINVAL;
525	}
526
527	tj9 = kzalloc(sizeof(*tj9), GFP_KERNEL);
528	if (!tj9) {
529		dev_err(&client->dev,
530			"failed to allocate memory for module data\n");
531		return -ENOMEM;
532	}
533
534	tj9->client = client;
535	tj9->pdata = *pdata;
536
537	if (pdata->init) {
538		err = pdata->init();
539		if (err < 0)
540			goto err_free_mem;
541	}
542
543	err = kxtj9_verify(tj9);
544	if (err < 0) {
545		dev_err(&client->dev, "device not recognized\n");
546		goto err_pdata_exit;
547	}
548
549	i2c_set_clientdata(client, tj9);
550
551	tj9->ctrl_reg1 = tj9->pdata.res_12bit | tj9->pdata.g_range;
552	tj9->last_poll_interval = tj9->pdata.init_interval;
553
554	if (client->irq) {
555		/* If in irq mode, populate INT_CTRL_REG1 and enable DRDY. */
556		tj9->int_ctrl |= KXTJ9_IEN | KXTJ9_IEA | KXTJ9_IEL;
557		tj9->ctrl_reg1 |= DRDYE;
558
559		err = kxtj9_setup_input_device(tj9);
560		if (err)
561			goto err_pdata_exit;
562
563		err = request_threaded_irq(client->irq, NULL, kxtj9_isr,
564					   IRQF_TRIGGER_RISING | IRQF_ONESHOT,
565					   "kxtj9-irq", tj9);
566		if (err) {
567			dev_err(&client->dev, "request irq failed: %d\n", err);
568			goto err_destroy_input;
569		}
570
571		err = sysfs_create_group(&client->dev.kobj, &kxtj9_attribute_group);
572		if (err) {
573			dev_err(&client->dev, "sysfs create failed: %d\n", err);
574			goto err_free_irq;
575		}
576
577	} else {
578		err = kxtj9_setup_polled_device(tj9);
579		if (err)
580			goto err_pdata_exit;
581	}
582
583	return 0;
584
585err_free_irq:
586	free_irq(client->irq, tj9);
587err_destroy_input:
588	input_unregister_device(tj9->input_dev);
589err_pdata_exit:
590	if (tj9->pdata.exit)
591		tj9->pdata.exit();
592err_free_mem:
593	kfree(tj9);
594	return err;
595}
596
597static int __devexit kxtj9_remove(struct i2c_client *client)
598{
599	struct kxtj9_data *tj9 = i2c_get_clientdata(client);
600
601	if (client->irq) {
602		sysfs_remove_group(&client->dev.kobj, &kxtj9_attribute_group);
603		free_irq(client->irq, tj9);
604		input_unregister_device(tj9->input_dev);
605	} else {
606		kxtj9_teardown_polled_device(tj9);
607	}
608
609	if (tj9->pdata.exit)
610		tj9->pdata.exit();
611
612	kfree(tj9);
613
614	return 0;
615}
616
617#ifdef CONFIG_PM_SLEEP
618static int kxtj9_suspend(struct device *dev)
619{
620	struct i2c_client *client = to_i2c_client(dev);
621	struct kxtj9_data *tj9 = i2c_get_clientdata(client);
622	struct input_dev *input_dev = tj9->input_dev;
623
624	mutex_lock(&input_dev->mutex);
625
626	if (input_dev->users)
627		kxtj9_disable(tj9);
628
629	mutex_unlock(&input_dev->mutex);
630	return 0;
631}
632
633static int kxtj9_resume(struct device *dev)
634{
635	struct i2c_client *client = to_i2c_client(dev);
636	struct kxtj9_data *tj9 = i2c_get_clientdata(client);
637	struct input_dev *input_dev = tj9->input_dev;
638	int retval = 0;
639
640	mutex_lock(&input_dev->mutex);
641
642	if (input_dev->users)
643		kxtj9_enable(tj9);
644
645	mutex_unlock(&input_dev->mutex);
646	return retval;
647}
648#endif
649
650static SIMPLE_DEV_PM_OPS(kxtj9_pm_ops, kxtj9_suspend, kxtj9_resume);
651
652static const struct i2c_device_id kxtj9_id[] = {
653	{ NAME, 0 },
654	{ },
655};
656
657MODULE_DEVICE_TABLE(i2c, kxtj9_id);
658
659static struct i2c_driver kxtj9_driver = {
660	.driver = {
661		.name	= NAME,
662		.owner	= THIS_MODULE,
663		.pm	= &kxtj9_pm_ops,
664	},
665	.probe		= kxtj9_probe,
666	.remove		= __devexit_p(kxtj9_remove),
667	.id_table	= kxtj9_id,
668};
669
670module_i2c_driver(kxtj9_driver);
671
672MODULE_DESCRIPTION("KXTJ9 accelerometer driver");
673MODULE_AUTHOR("Chris Hudson <chudson@kionix.com>");
674MODULE_LICENSE("GPL");
675