1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2011 Kionix, Inc.
4  * Written by Chris Hudson <chudson@kionix.com>
5  */
6 
7 #include <linux/delay.h>
8 #include <linux/i2c.h>
9 #include <linux/input.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/input/kxtj9.h>
14 
15 #define NAME			"kxtj9"
16 #define G_MAX			8000
17 /* OUTPUT REGISTERS */
18 #define XOUT_L			0x06
19 #define WHO_AM_I		0x0F
20 /* CONTROL REGISTERS */
21 #define INT_REL			0x1A
22 #define CTRL_REG1		0x1B
23 #define INT_CTRL1		0x1E
24 #define DATA_CTRL		0x21
25 /* CONTROL REGISTER 1 BITS */
26 #define PC1_OFF			0x7F
27 #define PC1_ON			(1 << 7)
28 /* Data ready funtion enable bit: set during probe if using irq mode */
29 #define DRDYE			(1 << 5)
30 /* DATA CONTROL REGISTER BITS */
31 #define ODR12_5F		0
32 #define ODR25F			1
33 #define ODR50F			2
34 #define ODR100F		3
35 #define ODR200F		4
36 #define ODR400F		5
37 #define ODR800F		6
38 /* INTERRUPT CONTROL REGISTER 1 BITS */
39 /* Set these during probe if using irq mode */
40 #define KXTJ9_IEL		(1 << 3)
41 #define KXTJ9_IEA		(1 << 4)
42 #define KXTJ9_IEN		(1 << 5)
43 /* INPUT_ABS CONSTANTS */
44 #define FUZZ			3
45 #define FLAT			3
46 /* RESUME STATE INDICES */
47 #define RES_DATA_CTRL		0
48 #define RES_CTRL_REG1		1
49 #define RES_INT_CTRL1		2
50 #define RESUME_ENTRIES		3
51 
52 /*
53  * The following table lists the maximum appropriate poll interval for each
54  * available output data rate.
55  */
56 static const struct {
57 	unsigned int cutoff;
58 	u8 mask;
59 } kxtj9_odr_table[] = {
60 	{ 3,	ODR800F },
61 	{ 5,	ODR400F },
62 	{ 10,	ODR200F },
63 	{ 20,	ODR100F },
64 	{ 40,	ODR50F  },
65 	{ 80,	ODR25F  },
66 	{ 0,	ODR12_5F},
67 };
68 
69 struct kxtj9_data {
70 	struct i2c_client *client;
71 	struct kxtj9_platform_data pdata;
72 	struct input_dev *input_dev;
73 	unsigned int last_poll_interval;
74 	u8 shift;
75 	u8 ctrl_reg1;
76 	u8 data_ctrl;
77 	u8 int_ctrl;
78 };
79 
kxtj9_i2c_read(struct kxtj9_data * tj9,u8 addr,u8 * data,int len)80 static int kxtj9_i2c_read(struct kxtj9_data *tj9, u8 addr, u8 *data, int len)
81 {
82 	struct i2c_msg msgs[] = {
83 		{
84 			.addr = tj9->client->addr,
85 			.flags = tj9->client->flags,
86 			.len = 1,
87 			.buf = &addr,
88 		},
89 		{
90 			.addr = tj9->client->addr,
91 			.flags = tj9->client->flags | I2C_M_RD,
92 			.len = len,
93 			.buf = data,
94 		},
95 	};
96 
97 	return i2c_transfer(tj9->client->adapter, msgs, 2);
98 }
99 
kxtj9_report_acceleration_data(struct kxtj9_data * tj9)100 static void kxtj9_report_acceleration_data(struct kxtj9_data *tj9)
101 {
102 	s16 acc_data[3]; /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
103 	s16 x, y, z;
104 	int err;
105 
106 	err = kxtj9_i2c_read(tj9, XOUT_L, (u8 *)acc_data, 6);
107 	if (err < 0)
108 		dev_err(&tj9->client->dev, "accelerometer data read failed\n");
109 
110 	x = le16_to_cpu(acc_data[tj9->pdata.axis_map_x]);
111 	y = le16_to_cpu(acc_data[tj9->pdata.axis_map_y]);
112 	z = le16_to_cpu(acc_data[tj9->pdata.axis_map_z]);
113 
114 	x >>= tj9->shift;
115 	y >>= tj9->shift;
116 	z >>= tj9->shift;
117 
118 	input_report_abs(tj9->input_dev, ABS_X, tj9->pdata.negate_x ? -x : x);
119 	input_report_abs(tj9->input_dev, ABS_Y, tj9->pdata.negate_y ? -y : y);
120 	input_report_abs(tj9->input_dev, ABS_Z, tj9->pdata.negate_z ? -z : z);
121 	input_sync(tj9->input_dev);
122 }
123 
kxtj9_isr(int irq,void * dev)124 static irqreturn_t kxtj9_isr(int irq, void *dev)
125 {
126 	struct kxtj9_data *tj9 = dev;
127 	int err;
128 
129 	/* data ready is the only possible interrupt type */
130 	kxtj9_report_acceleration_data(tj9);
131 
132 	err = i2c_smbus_read_byte_data(tj9->client, INT_REL);
133 	if (err < 0)
134 		dev_err(&tj9->client->dev,
135 			"error clearing interrupt status: %d\n", err);
136 
137 	return IRQ_HANDLED;
138 }
139 
kxtj9_update_g_range(struct kxtj9_data * tj9,u8 new_g_range)140 static int kxtj9_update_g_range(struct kxtj9_data *tj9, u8 new_g_range)
141 {
142 	switch (new_g_range) {
143 	case KXTJ9_G_2G:
144 		tj9->shift = 4;
145 		break;
146 	case KXTJ9_G_4G:
147 		tj9->shift = 3;
148 		break;
149 	case KXTJ9_G_8G:
150 		tj9->shift = 2;
151 		break;
152 	default:
153 		return -EINVAL;
154 	}
155 
156 	tj9->ctrl_reg1 &= 0xe7;
157 	tj9->ctrl_reg1 |= new_g_range;
158 
159 	return 0;
160 }
161 
kxtj9_update_odr(struct kxtj9_data * tj9,unsigned int poll_interval)162 static int kxtj9_update_odr(struct kxtj9_data *tj9, unsigned int poll_interval)
163 {
164 	int err;
165 	int i;
166 
167 	/* Use the lowest ODR that can support the requested poll interval */
168 	for (i = 0; i < ARRAY_SIZE(kxtj9_odr_table); i++) {
169 		tj9->data_ctrl = kxtj9_odr_table[i].mask;
170 		if (poll_interval < kxtj9_odr_table[i].cutoff)
171 			break;
172 	}
173 
174 	err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, 0);
175 	if (err < 0)
176 		return err;
177 
178 	err = i2c_smbus_write_byte_data(tj9->client, DATA_CTRL, tj9->data_ctrl);
179 	if (err < 0)
180 		return err;
181 
182 	err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1);
183 	if (err < 0)
184 		return err;
185 
186 	return 0;
187 }
188 
kxtj9_device_power_on(struct kxtj9_data * tj9)189 static int kxtj9_device_power_on(struct kxtj9_data *tj9)
190 {
191 	if (tj9->pdata.power_on)
192 		return tj9->pdata.power_on();
193 
194 	return 0;
195 }
196 
kxtj9_device_power_off(struct kxtj9_data * tj9)197 static void kxtj9_device_power_off(struct kxtj9_data *tj9)
198 {
199 	int err;
200 
201 	tj9->ctrl_reg1 &= PC1_OFF;
202 	err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1);
203 	if (err < 0)
204 		dev_err(&tj9->client->dev, "soft power off failed\n");
205 
206 	if (tj9->pdata.power_off)
207 		tj9->pdata.power_off();
208 }
209 
kxtj9_enable(struct kxtj9_data * tj9)210 static int kxtj9_enable(struct kxtj9_data *tj9)
211 {
212 	int err;
213 
214 	err = kxtj9_device_power_on(tj9);
215 	if (err < 0)
216 		return err;
217 
218 	/* ensure that PC1 is cleared before updating control registers */
219 	err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, 0);
220 	if (err < 0)
221 		return err;
222 
223 	/* only write INT_CTRL_REG1 if in irq mode */
224 	if (tj9->client->irq) {
225 		err = i2c_smbus_write_byte_data(tj9->client,
226 						INT_CTRL1, tj9->int_ctrl);
227 		if (err < 0)
228 			return err;
229 	}
230 
231 	err = kxtj9_update_g_range(tj9, tj9->pdata.g_range);
232 	if (err < 0)
233 		return err;
234 
235 	/* turn on outputs */
236 	tj9->ctrl_reg1 |= PC1_ON;
237 	err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1);
238 	if (err < 0)
239 		return err;
240 
241 	err = kxtj9_update_odr(tj9, tj9->last_poll_interval);
242 	if (err < 0)
243 		return err;
244 
245 	/* clear initial interrupt if in irq mode */
246 	if (tj9->client->irq) {
247 		err = i2c_smbus_read_byte_data(tj9->client, INT_REL);
248 		if (err < 0) {
249 			dev_err(&tj9->client->dev,
250 				"error clearing interrupt: %d\n", err);
251 			goto fail;
252 		}
253 	}
254 
255 	return 0;
256 
257 fail:
258 	kxtj9_device_power_off(tj9);
259 	return err;
260 }
261 
kxtj9_disable(struct kxtj9_data * tj9)262 static void kxtj9_disable(struct kxtj9_data *tj9)
263 {
264 	kxtj9_device_power_off(tj9);
265 }
266 
kxtj9_input_open(struct input_dev * input)267 static int kxtj9_input_open(struct input_dev *input)
268 {
269 	struct kxtj9_data *tj9 = input_get_drvdata(input);
270 
271 	return kxtj9_enable(tj9);
272 }
273 
kxtj9_input_close(struct input_dev * dev)274 static void kxtj9_input_close(struct input_dev *dev)
275 {
276 	struct kxtj9_data *tj9 = input_get_drvdata(dev);
277 
278 	kxtj9_disable(tj9);
279 }
280 
281 /*
282  * When IRQ mode is selected, we need to provide an interface to allow the user
283  * to change the output data rate of the part.  For consistency, we are using
284  * the set_poll method, which accepts a poll interval in milliseconds, and then
285  * calls update_odr() while passing this value as an argument.  In IRQ mode, the
286  * data outputs will not be read AT the requested poll interval, rather, the
287  * lowest ODR that can support the requested interval.  The client application
288  * will be responsible for retrieving data from the input node at the desired
289  * interval.
290  */
291 
292 /* Returns currently selected poll interval (in ms) */
kxtj9_get_poll(struct device * dev,struct device_attribute * attr,char * buf)293 static ssize_t kxtj9_get_poll(struct device *dev,
294 				struct device_attribute *attr, char *buf)
295 {
296 	struct i2c_client *client = to_i2c_client(dev);
297 	struct kxtj9_data *tj9 = i2c_get_clientdata(client);
298 
299 	return sprintf(buf, "%d\n", tj9->last_poll_interval);
300 }
301 
302 /* Allow users to select a new poll interval (in ms) */
kxtj9_set_poll(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)303 static ssize_t kxtj9_set_poll(struct device *dev, struct device_attribute *attr,
304 						const char *buf, size_t count)
305 {
306 	struct i2c_client *client = to_i2c_client(dev);
307 	struct kxtj9_data *tj9 = i2c_get_clientdata(client);
308 	struct input_dev *input_dev = tj9->input_dev;
309 	unsigned int interval;
310 	int error;
311 
312 	error = kstrtouint(buf, 10, &interval);
313 	if (error < 0)
314 		return error;
315 
316 	/* Lock the device to prevent races with open/close (and itself) */
317 	mutex_lock(&input_dev->mutex);
318 
319 	disable_irq(client->irq);
320 
321 	/*
322 	 * Set current interval to the greater of the minimum interval or
323 	 * the requested interval
324 	 */
325 	tj9->last_poll_interval = max(interval, tj9->pdata.min_interval);
326 
327 	kxtj9_update_odr(tj9, tj9->last_poll_interval);
328 
329 	enable_irq(client->irq);
330 	mutex_unlock(&input_dev->mutex);
331 
332 	return count;
333 }
334 
335 static DEVICE_ATTR(poll, S_IRUGO|S_IWUSR, kxtj9_get_poll, kxtj9_set_poll);
336 
337 static struct attribute *kxtj9_attrs[] = {
338 	&dev_attr_poll.attr,
339 	NULL
340 };
341 
kxtj9_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)342 static umode_t kxtj9_attr_is_visible(struct kobject *kobj,
343 				     struct attribute *attr, int n)
344 {
345 	struct device *dev = kobj_to_dev(kobj);
346 	struct i2c_client *client = to_i2c_client(dev);
347 
348 	return client->irq ? attr->mode : 0;
349 }
350 
351 static struct attribute_group kxtj9_group = {
352 	.attrs = kxtj9_attrs,
353 	.is_visible = kxtj9_attr_is_visible,
354 };
355 __ATTRIBUTE_GROUPS(kxtj9);
356 
kxtj9_poll(struct input_dev * input)357 static void kxtj9_poll(struct input_dev *input)
358 {
359 	struct kxtj9_data *tj9 = input_get_drvdata(input);
360 	unsigned int poll_interval = input_get_poll_interval(input);
361 
362 	kxtj9_report_acceleration_data(tj9);
363 
364 	if (poll_interval != tj9->last_poll_interval) {
365 		kxtj9_update_odr(tj9, poll_interval);
366 		tj9->last_poll_interval = poll_interval;
367 	}
368 }
369 
kxtj9_platform_exit(void * data)370 static void kxtj9_platform_exit(void *data)
371 {
372 	struct kxtj9_data *tj9 = data;
373 
374 	if (tj9->pdata.exit)
375 		tj9->pdata.exit();
376 }
377 
kxtj9_verify(struct kxtj9_data * tj9)378 static int kxtj9_verify(struct kxtj9_data *tj9)
379 {
380 	int retval;
381 
382 	retval = kxtj9_device_power_on(tj9);
383 	if (retval < 0)
384 		return retval;
385 
386 	retval = i2c_smbus_read_byte_data(tj9->client, WHO_AM_I);
387 	if (retval < 0) {
388 		dev_err(&tj9->client->dev, "read err int source\n");
389 		goto out;
390 	}
391 
392 	retval = (retval != 0x07 && retval != 0x08) ? -EIO : 0;
393 
394 out:
395 	kxtj9_device_power_off(tj9);
396 	return retval;
397 }
398 
kxtj9_probe(struct i2c_client * client)399 static int kxtj9_probe(struct i2c_client *client)
400 {
401 	const struct kxtj9_platform_data *pdata =
402 			dev_get_platdata(&client->dev);
403 	struct kxtj9_data *tj9;
404 	struct input_dev *input_dev;
405 	int err;
406 
407 	if (!i2c_check_functionality(client->adapter,
408 				I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE_DATA)) {
409 		dev_err(&client->dev, "client is not i2c capable\n");
410 		return -ENXIO;
411 	}
412 
413 	if (!pdata) {
414 		dev_err(&client->dev, "platform data is NULL; exiting\n");
415 		return -EINVAL;
416 	}
417 
418 	tj9 = devm_kzalloc(&client->dev, sizeof(*tj9), GFP_KERNEL);
419 	if (!tj9) {
420 		dev_err(&client->dev,
421 			"failed to allocate memory for module data\n");
422 		return -ENOMEM;
423 	}
424 
425 	tj9->client = client;
426 	tj9->pdata = *pdata;
427 
428 	if (pdata->init) {
429 		err = pdata->init();
430 		if (err < 0)
431 			return err;
432 	}
433 
434 	err = devm_add_action_or_reset(&client->dev, kxtj9_platform_exit, tj9);
435 	if (err)
436 		return err;
437 
438 	err = kxtj9_verify(tj9);
439 	if (err < 0) {
440 		dev_err(&client->dev, "device not recognized\n");
441 		return err;
442 	}
443 
444 	i2c_set_clientdata(client, tj9);
445 
446 	tj9->ctrl_reg1 = tj9->pdata.res_12bit | tj9->pdata.g_range;
447 	tj9->last_poll_interval = tj9->pdata.init_interval;
448 
449 	input_dev = devm_input_allocate_device(&client->dev);
450 	if (!input_dev) {
451 		dev_err(&client->dev, "input device allocate failed\n");
452 		return -ENOMEM;
453 	}
454 
455 	input_set_drvdata(input_dev, tj9);
456 	tj9->input_dev = input_dev;
457 
458 	input_dev->name = "kxtj9_accel";
459 	input_dev->id.bustype = BUS_I2C;
460 
461 	input_dev->open = kxtj9_input_open;
462 	input_dev->close = kxtj9_input_close;
463 
464 	input_set_abs_params(input_dev, ABS_X, -G_MAX, G_MAX, FUZZ, FLAT);
465 	input_set_abs_params(input_dev, ABS_Y, -G_MAX, G_MAX, FUZZ, FLAT);
466 	input_set_abs_params(input_dev, ABS_Z, -G_MAX, G_MAX, FUZZ, FLAT);
467 
468 	if (client->irq <= 0) {
469 		err = input_setup_polling(input_dev, kxtj9_poll);
470 		if (err)
471 			return err;
472 	}
473 
474 	err = input_register_device(input_dev);
475 	if (err) {
476 		dev_err(&client->dev,
477 			"unable to register input polled device %s: %d\n",
478 			input_dev->name, err);
479 		return err;
480 	}
481 
482 	if (client->irq) {
483 		/* If in irq mode, populate INT_CTRL_REG1 and enable DRDY. */
484 		tj9->int_ctrl |= KXTJ9_IEN | KXTJ9_IEA | KXTJ9_IEL;
485 		tj9->ctrl_reg1 |= DRDYE;
486 
487 		err = devm_request_threaded_irq(&client->dev, client->irq,
488 						NULL, kxtj9_isr,
489 						IRQF_TRIGGER_RISING |
490 							IRQF_ONESHOT,
491 						"kxtj9-irq", tj9);
492 		if (err) {
493 			dev_err(&client->dev, "request irq failed: %d\n", err);
494 			return err;
495 		}
496 	}
497 
498 	return 0;
499 }
500 
kxtj9_suspend(struct device * dev)501 static int kxtj9_suspend(struct device *dev)
502 {
503 	struct i2c_client *client = to_i2c_client(dev);
504 	struct kxtj9_data *tj9 = i2c_get_clientdata(client);
505 	struct input_dev *input_dev = tj9->input_dev;
506 
507 	mutex_lock(&input_dev->mutex);
508 
509 	if (input_device_enabled(input_dev))
510 		kxtj9_disable(tj9);
511 
512 	mutex_unlock(&input_dev->mutex);
513 	return 0;
514 }
515 
kxtj9_resume(struct device * dev)516 static int kxtj9_resume(struct device *dev)
517 {
518 	struct i2c_client *client = to_i2c_client(dev);
519 	struct kxtj9_data *tj9 = i2c_get_clientdata(client);
520 	struct input_dev *input_dev = tj9->input_dev;
521 
522 	mutex_lock(&input_dev->mutex);
523 
524 	if (input_device_enabled(input_dev))
525 		kxtj9_enable(tj9);
526 
527 	mutex_unlock(&input_dev->mutex);
528 	return 0;
529 }
530 
531 static DEFINE_SIMPLE_DEV_PM_OPS(kxtj9_pm_ops, kxtj9_suspend, kxtj9_resume);
532 
533 static const struct i2c_device_id kxtj9_id[] = {
534 	{ NAME },
535 	{ }
536 };
537 
538 MODULE_DEVICE_TABLE(i2c, kxtj9_id);
539 
540 static struct i2c_driver kxtj9_driver = {
541 	.driver = {
542 		.name		= NAME,
543 		.dev_groups	= kxtj9_groups,
544 		.pm		= pm_sleep_ptr(&kxtj9_pm_ops),
545 	},
546 	.probe		= kxtj9_probe,
547 	.id_table	= kxtj9_id,
548 };
549 
550 module_i2c_driver(kxtj9_driver);
551 
552 MODULE_DESCRIPTION("KXTJ9 accelerometer driver");
553 MODULE_AUTHOR("Chris Hudson <chudson@kionix.com>");
554 MODULE_LICENSE("GPL");
555