drv2667.c 12 KB

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