kxtj9.c 15 KB

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