drv2667.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * DRV2667 haptics driver family
  3. *
  4. * Author: Dan Murphy <dmurphy@ti.com>
  5. *
  6. * Copyright: (C) 2014 Texas Instruments, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include <linux/i2c.h>
  18. #include <linux/input.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. #include <linux/delay.h>
  24. #include <linux/regulator/consumer.h>
  25. /* Contol registers */
  26. #define DRV2667_STATUS 0x00
  27. #define DRV2667_CTRL_1 0x01
  28. #define DRV2667_CTRL_2 0x02
  29. /* Waveform sequencer */
  30. #define DRV2667_WV_SEQ_0 0x03
  31. #define DRV2667_WV_SEQ_1 0x04
  32. #define DRV2667_WV_SEQ_2 0x05
  33. #define DRV2667_WV_SEQ_3 0x06
  34. #define DRV2667_WV_SEQ_4 0x07
  35. #define DRV2667_WV_SEQ_5 0x08
  36. #define DRV2667_WV_SEQ_6 0x09
  37. #define DRV2667_WV_SEQ_7 0x0A
  38. #define DRV2667_FIFO 0x0B
  39. #define DRV2667_PAGE 0xFF
  40. #define DRV2667_MAX_REG DRV2667_PAGE
  41. #define DRV2667_PAGE_0 0x00
  42. #define DRV2667_PAGE_1 0x01
  43. #define DRV2667_PAGE_2 0x02
  44. #define DRV2667_PAGE_3 0x03
  45. #define DRV2667_PAGE_4 0x04
  46. #define DRV2667_PAGE_5 0x05
  47. #define DRV2667_PAGE_6 0x06
  48. #define DRV2667_PAGE_7 0x07
  49. #define DRV2667_PAGE_8 0x08
  50. /* RAM fields */
  51. #define DRV2667_RAM_HDR_SZ 0x0
  52. /* RAM Header addresses */
  53. #define DRV2667_RAM_START_HI 0x01
  54. #define DRV2667_RAM_START_LO 0x02
  55. #define DRV2667_RAM_STOP_HI 0x03
  56. #define DRV2667_RAM_STOP_LO 0x04
  57. #define DRV2667_RAM_REPEAT_CT 0x05
  58. /* RAM data addresses */
  59. #define DRV2667_RAM_AMP 0x06
  60. #define DRV2667_RAM_FREQ 0x07
  61. #define DRV2667_RAM_DURATION 0x08
  62. #define DRV2667_RAM_ENVELOPE 0x09
  63. /* Control 1 Register */
  64. #define DRV2667_25_VPP_GAIN 0x00
  65. #define DRV2667_50_VPP_GAIN 0x01
  66. #define DRV2667_75_VPP_GAIN 0x02
  67. #define DRV2667_100_VPP_GAIN 0x03
  68. #define DRV2667_DIGITAL_IN 0xfc
  69. #define DRV2667_ANALOG_IN (1 << 2)
  70. /* Control 2 Register */
  71. #define DRV2667_GO (1 << 0)
  72. #define DRV2667_STANDBY (1 << 6)
  73. #define DRV2667_DEV_RST (1 << 7)
  74. /* RAM Envelope settings */
  75. #define DRV2667_NO_ENV 0x00
  76. #define DRV2667_32_MS_ENV 0x01
  77. #define DRV2667_64_MS_ENV 0x02
  78. #define DRV2667_96_MS_ENV 0x03
  79. #define DRV2667_128_MS_ENV 0x04
  80. #define DRV2667_160_MS_ENV 0x05
  81. #define DRV2667_192_MS_ENV 0x06
  82. #define DRV2667_224_MS_ENV 0x07
  83. #define DRV2667_256_MS_ENV 0x08
  84. #define DRV2667_512_MS_ENV 0x09
  85. #define DRV2667_768_MS_ENV 0x0a
  86. #define DRV2667_1024_MS_ENV 0x0b
  87. #define DRV2667_1280_MS_ENV 0x0c
  88. #define DRV2667_1536_MS_ENV 0x0d
  89. #define DRV2667_1792_MS_ENV 0x0e
  90. #define DRV2667_2048_MS_ENV 0x0f
  91. /**
  92. * struct drv2667_data -
  93. * @input_dev - Pointer to the input device
  94. * @client - Pointer to the I2C client
  95. * @regmap - Register map of the device
  96. * @work - Work item used to off load the enable/disable of the vibration
  97. * @regulator - Pointer to the regulator for the IC
  98. * @magnitude - Magnitude of the vibration event
  99. **/
  100. struct drv2667_data {
  101. struct input_dev *input_dev;
  102. struct i2c_client *client;
  103. struct regmap *regmap;
  104. struct work_struct work;
  105. struct regulator *regulator;
  106. u32 page;
  107. u32 magnitude;
  108. u32 frequency;
  109. };
  110. static const struct reg_default drv2667_reg_defs[] = {
  111. { DRV2667_STATUS, 0x02 },
  112. { DRV2667_CTRL_1, 0x28 },
  113. { DRV2667_CTRL_2, 0x40 },
  114. { DRV2667_WV_SEQ_0, 0x00 },
  115. { DRV2667_WV_SEQ_1, 0x00 },
  116. { DRV2667_WV_SEQ_2, 0x00 },
  117. { DRV2667_WV_SEQ_3, 0x00 },
  118. { DRV2667_WV_SEQ_4, 0x00 },
  119. { DRV2667_WV_SEQ_5, 0x00 },
  120. { DRV2667_WV_SEQ_6, 0x00 },
  121. { DRV2667_WV_SEQ_7, 0x00 },
  122. { DRV2667_FIFO, 0x00 },
  123. { DRV2667_PAGE, 0x00 },
  124. };
  125. static int drv2667_set_waveform_freq(struct drv2667_data *haptics)
  126. {
  127. unsigned int read_buf;
  128. int freq;
  129. int error;
  130. /* Per the data sheet:
  131. * Sinusoid Frequency (Hz) = 7.8125 x Frequency
  132. */
  133. freq = (haptics->frequency * 1000) / 78125;
  134. if (freq <= 0) {
  135. dev_err(&haptics->client->dev,
  136. "ERROR: Frequency calculated to %i\n", freq);
  137. return -EINVAL;
  138. }
  139. error = regmap_read(haptics->regmap, DRV2667_PAGE, &read_buf);
  140. if (error) {
  141. dev_err(&haptics->client->dev,
  142. "Failed to read the page number: %d\n", error);
  143. return -EIO;
  144. }
  145. if (read_buf == DRV2667_PAGE_0 ||
  146. haptics->page != read_buf) {
  147. error = regmap_write(haptics->regmap,
  148. DRV2667_PAGE, haptics->page);
  149. if (error) {
  150. dev_err(&haptics->client->dev,
  151. "Failed to set the page: %d\n", error);
  152. return -EIO;
  153. }
  154. }
  155. error = regmap_write(haptics->regmap, DRV2667_RAM_FREQ, freq);
  156. if (error)
  157. dev_err(&haptics->client->dev,
  158. "Failed to set the frequency: %d\n", error);
  159. /* Reset back to original page */
  160. if (read_buf == DRV2667_PAGE_0 ||
  161. haptics->page != read_buf) {
  162. error = regmap_write(haptics->regmap, DRV2667_PAGE, read_buf);
  163. if (error) {
  164. dev_err(&haptics->client->dev,
  165. "Failed to set the page: %d\n", error);
  166. return -EIO;
  167. }
  168. }
  169. return error;
  170. }
  171. static void drv2667_worker(struct work_struct *work)
  172. {
  173. struct drv2667_data *haptics = container_of(work, struct drv2667_data, work);
  174. int error;
  175. if (haptics->magnitude) {
  176. error = regmap_write(haptics->regmap,
  177. DRV2667_PAGE, haptics->page);
  178. if (error) {
  179. dev_err(&haptics->client->dev,
  180. "Failed to set the page: %d\n", error);
  181. return;
  182. }
  183. error = regmap_write(haptics->regmap, DRV2667_RAM_AMP,
  184. haptics->magnitude);
  185. if (error) {
  186. dev_err(&haptics->client->dev,
  187. "Failed to set the amplitude: %d\n", error);
  188. return;
  189. }
  190. error = regmap_write(haptics->regmap,
  191. DRV2667_PAGE, DRV2667_PAGE_0);
  192. if (error) {
  193. dev_err(&haptics->client->dev,
  194. "Failed to set the page: %d\n", error);
  195. return;
  196. }
  197. error = regmap_write(haptics->regmap,
  198. DRV2667_CTRL_2, DRV2667_GO);
  199. if (error) {
  200. dev_err(&haptics->client->dev,
  201. "Failed to set the GO bit: %d\n", error);
  202. }
  203. } else {
  204. error = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
  205. DRV2667_GO, 0);
  206. if (error) {
  207. dev_err(&haptics->client->dev,
  208. "Failed to unset the GO bit: %d\n", error);
  209. }
  210. }
  211. }
  212. static int drv2667_haptics_play(struct input_dev *input, void *data,
  213. struct ff_effect *effect)
  214. {
  215. struct drv2667_data *haptics = input_get_drvdata(input);
  216. if (effect->u.rumble.strong_magnitude > 0)
  217. haptics->magnitude = effect->u.rumble.strong_magnitude;
  218. else if (effect->u.rumble.weak_magnitude > 0)
  219. haptics->magnitude = effect->u.rumble.weak_magnitude;
  220. else
  221. haptics->magnitude = 0;
  222. schedule_work(&haptics->work);
  223. return 0;
  224. }
  225. static void drv2667_close(struct input_dev *input)
  226. {
  227. struct drv2667_data *haptics = input_get_drvdata(input);
  228. int error;
  229. cancel_work_sync(&haptics->work);
  230. error = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
  231. DRV2667_STANDBY, 1);
  232. if (error)
  233. dev_err(&haptics->client->dev,
  234. "Failed to enter standby mode: %d\n", error);
  235. }
  236. static const struct reg_sequence drv2667_init_regs[] = {
  237. { DRV2667_CTRL_2, 0 },
  238. { DRV2667_CTRL_1, DRV2667_25_VPP_GAIN },
  239. { DRV2667_WV_SEQ_0, 1 },
  240. { DRV2667_WV_SEQ_1, 0 }
  241. };
  242. static const struct reg_sequence drv2667_page1_init[] = {
  243. { DRV2667_RAM_HDR_SZ, 0x05 },
  244. { DRV2667_RAM_START_HI, 0x80 },
  245. { DRV2667_RAM_START_LO, 0x06 },
  246. { DRV2667_RAM_STOP_HI, 0x00 },
  247. { DRV2667_RAM_STOP_LO, 0x09 },
  248. { DRV2667_RAM_REPEAT_CT, 0 },
  249. { DRV2667_RAM_DURATION, 0x05 },
  250. { DRV2667_RAM_ENVELOPE, DRV2667_NO_ENV },
  251. { DRV2667_RAM_AMP, 0x60 },
  252. };
  253. static int drv2667_init(struct drv2667_data *haptics)
  254. {
  255. int error;
  256. /* Set default haptic frequency to 195Hz on Page 1*/
  257. haptics->frequency = 195;
  258. haptics->page = DRV2667_PAGE_1;
  259. error = regmap_register_patch(haptics->regmap,
  260. drv2667_init_regs,
  261. ARRAY_SIZE(drv2667_init_regs));
  262. if (error) {
  263. dev_err(&haptics->client->dev,
  264. "Failed to write init registers: %d\n",
  265. error);
  266. return error;
  267. }
  268. error = regmap_write(haptics->regmap, DRV2667_PAGE, haptics->page);
  269. if (error) {
  270. dev_err(&haptics->client->dev, "Failed to set page: %d\n",
  271. error);
  272. goto error_out;
  273. }
  274. error = drv2667_set_waveform_freq(haptics);
  275. if (error)
  276. goto error_page;
  277. error = regmap_register_patch(haptics->regmap,
  278. drv2667_page1_init,
  279. ARRAY_SIZE(drv2667_page1_init));
  280. if (error) {
  281. dev_err(&haptics->client->dev,
  282. "Failed to write page registers: %d\n",
  283. error);
  284. return error;
  285. }
  286. error = regmap_write(haptics->regmap, DRV2667_PAGE, DRV2667_PAGE_0);
  287. return error;
  288. error_page:
  289. regmap_write(haptics->regmap, DRV2667_PAGE, DRV2667_PAGE_0);
  290. error_out:
  291. return error;
  292. }
  293. static const struct regmap_config drv2667_regmap_config = {
  294. .reg_bits = 8,
  295. .val_bits = 8,
  296. .max_register = DRV2667_MAX_REG,
  297. .reg_defaults = drv2667_reg_defs,
  298. .num_reg_defaults = ARRAY_SIZE(drv2667_reg_defs),
  299. .cache_type = REGCACHE_NONE,
  300. };
  301. static int drv2667_probe(struct i2c_client *client,
  302. const struct i2c_device_id *id)
  303. {
  304. struct drv2667_data *haptics;
  305. int error;
  306. haptics = devm_kzalloc(&client->dev, sizeof(*haptics), GFP_KERNEL);
  307. if (!haptics)
  308. return -ENOMEM;
  309. haptics->regulator = devm_regulator_get(&client->dev, "vbat");
  310. if (IS_ERR(haptics->regulator)) {
  311. error = PTR_ERR(haptics->regulator);
  312. dev_err(&client->dev,
  313. "unable to get regulator, error: %d\n", error);
  314. return error;
  315. }
  316. haptics->input_dev = devm_input_allocate_device(&client->dev);
  317. if (!haptics->input_dev) {
  318. dev_err(&client->dev, "Failed to allocate input device\n");
  319. return -ENOMEM;
  320. }
  321. haptics->input_dev->name = "drv2667:haptics";
  322. haptics->input_dev->dev.parent = client->dev.parent;
  323. haptics->input_dev->close = drv2667_close;
  324. input_set_drvdata(haptics->input_dev, haptics);
  325. input_set_capability(haptics->input_dev, EV_FF, FF_RUMBLE);
  326. error = input_ff_create_memless(haptics->input_dev, NULL,
  327. drv2667_haptics_play);
  328. if (error) {
  329. dev_err(&client->dev, "input_ff_create() failed: %d\n",
  330. error);
  331. return error;
  332. }
  333. INIT_WORK(&haptics->work, drv2667_worker);
  334. haptics->client = client;
  335. i2c_set_clientdata(client, haptics);
  336. haptics->regmap = devm_regmap_init_i2c(client, &drv2667_regmap_config);
  337. if (IS_ERR(haptics->regmap)) {
  338. error = PTR_ERR(haptics->regmap);
  339. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  340. error);
  341. return error;
  342. }
  343. error = drv2667_init(haptics);
  344. if (error) {
  345. dev_err(&client->dev, "Device init failed: %d\n", error);
  346. return error;
  347. }
  348. error = input_register_device(haptics->input_dev);
  349. if (error) {
  350. dev_err(&client->dev, "couldn't register input device: %d\n",
  351. error);
  352. return error;
  353. }
  354. return 0;
  355. }
  356. static int __maybe_unused drv2667_suspend(struct device *dev)
  357. {
  358. struct drv2667_data *haptics = dev_get_drvdata(dev);
  359. int ret = 0;
  360. mutex_lock(&haptics->input_dev->mutex);
  361. if (haptics->input_dev->users) {
  362. ret = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
  363. DRV2667_STANDBY, 1);
  364. if (ret) {
  365. dev_err(dev, "Failed to set standby mode\n");
  366. regulator_disable(haptics->regulator);
  367. goto out;
  368. }
  369. ret = regulator_disable(haptics->regulator);
  370. if (ret) {
  371. dev_err(dev, "Failed to disable regulator\n");
  372. regmap_update_bits(haptics->regmap,
  373. DRV2667_CTRL_2,
  374. DRV2667_STANDBY, 0);
  375. }
  376. }
  377. out:
  378. mutex_unlock(&haptics->input_dev->mutex);
  379. return ret;
  380. }
  381. static int __maybe_unused drv2667_resume(struct device *dev)
  382. {
  383. struct drv2667_data *haptics = dev_get_drvdata(dev);
  384. int ret = 0;
  385. mutex_lock(&haptics->input_dev->mutex);
  386. if (haptics->input_dev->users) {
  387. ret = regulator_enable(haptics->regulator);
  388. if (ret) {
  389. dev_err(dev, "Failed to enable regulator\n");
  390. goto out;
  391. }
  392. ret = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
  393. DRV2667_STANDBY, 0);
  394. if (ret) {
  395. dev_err(dev, "Failed to unset standby mode\n");
  396. regulator_disable(haptics->regulator);
  397. goto out;
  398. }
  399. }
  400. out:
  401. mutex_unlock(&haptics->input_dev->mutex);
  402. return ret;
  403. }
  404. static SIMPLE_DEV_PM_OPS(drv2667_pm_ops, drv2667_suspend, drv2667_resume);
  405. static const struct i2c_device_id drv2667_id[] = {
  406. { "drv2667", 0 },
  407. { }
  408. };
  409. MODULE_DEVICE_TABLE(i2c, drv2667_id);
  410. #ifdef CONFIG_OF
  411. static const struct of_device_id drv2667_of_match[] = {
  412. { .compatible = "ti,drv2667", },
  413. { }
  414. };
  415. MODULE_DEVICE_TABLE(of, drv2667_of_match);
  416. #endif
  417. static struct i2c_driver drv2667_driver = {
  418. .probe = drv2667_probe,
  419. .driver = {
  420. .name = "drv2667-haptics",
  421. .of_match_table = of_match_ptr(drv2667_of_match),
  422. .pm = &drv2667_pm_ops,
  423. },
  424. .id_table = drv2667_id,
  425. };
  426. module_i2c_driver(drv2667_driver);
  427. MODULE_DESCRIPTION("TI DRV2667 haptics driver");
  428. MODULE_LICENSE("GPL");
  429. MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");