drv2665.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * DRV2665 haptics driver family
  4. *
  5. * Author: Dan Murphy <dmurphy@ti.com>
  6. *
  7. * Copyright: (C) 2015 Texas Instruments, Inc.
  8. */
  9. #include <linux/i2c.h>
  10. #include <linux/input.h>
  11. #include <linux/module.h>
  12. #include <linux/regmap.h>
  13. #include <linux/slab.h>
  14. #include <linux/delay.h>
  15. #include <linux/regulator/consumer.h>
  16. /* Contol registers */
  17. #define DRV2665_STATUS 0x00
  18. #define DRV2665_CTRL_1 0x01
  19. #define DRV2665_CTRL_2 0x02
  20. #define DRV2665_FIFO 0x0b
  21. /* Status Register */
  22. #define DRV2665_FIFO_FULL BIT(0)
  23. #define DRV2665_FIFO_EMPTY BIT(1)
  24. /* Control 1 Register */
  25. #define DRV2665_25_VPP_GAIN 0x00
  26. #define DRV2665_50_VPP_GAIN 0x01
  27. #define DRV2665_75_VPP_GAIN 0x02
  28. #define DRV2665_100_VPP_GAIN 0x03
  29. #define DRV2665_DIGITAL_IN 0xfc
  30. #define DRV2665_ANALOG_IN BIT(2)
  31. /* Control 2 Register */
  32. #define DRV2665_BOOST_EN BIT(1)
  33. #define DRV2665_STANDBY BIT(6)
  34. #define DRV2665_DEV_RST BIT(7)
  35. #define DRV2665_5_MS_IDLE_TOUT 0x00
  36. #define DRV2665_10_MS_IDLE_TOUT 0x04
  37. #define DRV2665_15_MS_IDLE_TOUT 0x08
  38. #define DRV2665_20_MS_IDLE_TOUT 0x0c
  39. /**
  40. * struct drv2665_data -
  41. * @input_dev: Pointer to the input device
  42. * @client: Pointer to the I2C client
  43. * @regmap: Register map of the device
  44. * @work: Work item used to off load the enable/disable of the vibration
  45. * @regulator: Pointer to the regulator for the IC
  46. */
  47. struct drv2665_data {
  48. struct input_dev *input_dev;
  49. struct i2c_client *client;
  50. struct regmap *regmap;
  51. struct work_struct work;
  52. struct regulator *regulator;
  53. };
  54. /* 8kHz Sine wave to stream to the FIFO */
  55. static const u8 drv2665_sine_wave_form[] = {
  56. 0x00, 0x10, 0x20, 0x2e, 0x3c, 0x48, 0x53, 0x5b, 0x61, 0x65, 0x66,
  57. 0x65, 0x61, 0x5b, 0x53, 0x48, 0x3c, 0x2e, 0x20, 0x10,
  58. 0x00, 0xf0, 0xe0, 0xd2, 0xc4, 0xb8, 0xad, 0xa5, 0x9f, 0x9b, 0x9a,
  59. 0x9b, 0x9f, 0xa5, 0xad, 0xb8, 0xc4, 0xd2, 0xe0, 0xf0, 0x00,
  60. };
  61. static const struct reg_default drv2665_reg_defs[] = {
  62. { DRV2665_STATUS, 0x02 },
  63. { DRV2665_CTRL_1, 0x28 },
  64. { DRV2665_CTRL_2, 0x40 },
  65. { DRV2665_FIFO, 0x00 },
  66. };
  67. static void drv2665_worker(struct work_struct *work)
  68. {
  69. struct drv2665_data *haptics =
  70. container_of(work, struct drv2665_data, work);
  71. unsigned int read_buf;
  72. int error;
  73. error = regmap_read(haptics->regmap, DRV2665_STATUS, &read_buf);
  74. if (error) {
  75. dev_err(&haptics->client->dev,
  76. "Failed to read status: %d\n", error);
  77. return;
  78. }
  79. if (read_buf & DRV2665_FIFO_EMPTY) {
  80. error = regmap_bulk_write(haptics->regmap,
  81. DRV2665_FIFO,
  82. drv2665_sine_wave_form,
  83. ARRAY_SIZE(drv2665_sine_wave_form));
  84. if (error) {
  85. dev_err(&haptics->client->dev,
  86. "Failed to write FIFO: %d\n", error);
  87. return;
  88. }
  89. }
  90. }
  91. static int drv2665_haptics_play(struct input_dev *input, void *data,
  92. struct ff_effect *effect)
  93. {
  94. struct drv2665_data *haptics = input_get_drvdata(input);
  95. schedule_work(&haptics->work);
  96. return 0;
  97. }
  98. static void drv2665_close(struct input_dev *input)
  99. {
  100. struct drv2665_data *haptics = input_get_drvdata(input);
  101. int error;
  102. cancel_work_sync(&haptics->work);
  103. error = regmap_update_bits(haptics->regmap, DRV2665_CTRL_2,
  104. DRV2665_STANDBY, DRV2665_STANDBY);
  105. if (error)
  106. dev_err(&haptics->client->dev,
  107. "Failed to enter standby mode: %d\n", error);
  108. }
  109. static const struct reg_sequence drv2665_init_regs[] = {
  110. { DRV2665_CTRL_2, 0 | DRV2665_10_MS_IDLE_TOUT },
  111. { DRV2665_CTRL_1, DRV2665_25_VPP_GAIN },
  112. };
  113. static int drv2665_init(struct drv2665_data *haptics)
  114. {
  115. int error;
  116. error = regmap_register_patch(haptics->regmap,
  117. drv2665_init_regs,
  118. ARRAY_SIZE(drv2665_init_regs));
  119. if (error) {
  120. dev_err(&haptics->client->dev,
  121. "Failed to write init registers: %d\n",
  122. error);
  123. return error;
  124. }
  125. return 0;
  126. }
  127. static const struct regmap_config drv2665_regmap_config = {
  128. .reg_bits = 8,
  129. .val_bits = 8,
  130. .max_register = DRV2665_FIFO,
  131. .reg_defaults = drv2665_reg_defs,
  132. .num_reg_defaults = ARRAY_SIZE(drv2665_reg_defs),
  133. .cache_type = REGCACHE_NONE,
  134. };
  135. static int drv2665_probe(struct i2c_client *client)
  136. {
  137. struct drv2665_data *haptics;
  138. int error;
  139. haptics = devm_kzalloc(&client->dev, sizeof(*haptics), GFP_KERNEL);
  140. if (!haptics)
  141. return -ENOMEM;
  142. haptics->regulator = devm_regulator_get(&client->dev, "vbat");
  143. if (IS_ERR(haptics->regulator)) {
  144. error = PTR_ERR(haptics->regulator);
  145. dev_err(&client->dev,
  146. "unable to get regulator, error: %d\n", error);
  147. return error;
  148. }
  149. haptics->input_dev = devm_input_allocate_device(&client->dev);
  150. if (!haptics->input_dev) {
  151. dev_err(&client->dev, "Failed to allocate input device\n");
  152. return -ENOMEM;
  153. }
  154. haptics->input_dev->name = "drv2665:haptics";
  155. haptics->input_dev->dev.parent = client->dev.parent;
  156. haptics->input_dev->close = drv2665_close;
  157. input_set_drvdata(haptics->input_dev, haptics);
  158. input_set_capability(haptics->input_dev, EV_FF, FF_RUMBLE);
  159. error = input_ff_create_memless(haptics->input_dev, NULL,
  160. drv2665_haptics_play);
  161. if (error) {
  162. dev_err(&client->dev, "input_ff_create() failed: %d\n",
  163. error);
  164. return error;
  165. }
  166. INIT_WORK(&haptics->work, drv2665_worker);
  167. haptics->client = client;
  168. i2c_set_clientdata(client, haptics);
  169. haptics->regmap = devm_regmap_init_i2c(client, &drv2665_regmap_config);
  170. if (IS_ERR(haptics->regmap)) {
  171. error = PTR_ERR(haptics->regmap);
  172. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  173. error);
  174. return error;
  175. }
  176. error = drv2665_init(haptics);
  177. if (error) {
  178. dev_err(&client->dev, "Device init failed: %d\n", error);
  179. return error;
  180. }
  181. error = input_register_device(haptics->input_dev);
  182. if (error) {
  183. dev_err(&client->dev, "couldn't register input device: %d\n",
  184. error);
  185. return error;
  186. }
  187. return 0;
  188. }
  189. static int drv2665_suspend(struct device *dev)
  190. {
  191. struct drv2665_data *haptics = dev_get_drvdata(dev);
  192. int ret = 0;
  193. mutex_lock(&haptics->input_dev->mutex);
  194. if (input_device_enabled(haptics->input_dev)) {
  195. ret = regmap_update_bits(haptics->regmap, DRV2665_CTRL_2,
  196. DRV2665_STANDBY, DRV2665_STANDBY);
  197. if (ret) {
  198. dev_err(dev, "Failed to set standby mode\n");
  199. regulator_disable(haptics->regulator);
  200. goto out;
  201. }
  202. ret = regulator_disable(haptics->regulator);
  203. if (ret) {
  204. dev_err(dev, "Failed to disable regulator\n");
  205. regmap_update_bits(haptics->regmap,
  206. DRV2665_CTRL_2,
  207. DRV2665_STANDBY, 0);
  208. }
  209. }
  210. out:
  211. mutex_unlock(&haptics->input_dev->mutex);
  212. return ret;
  213. }
  214. static int drv2665_resume(struct device *dev)
  215. {
  216. struct drv2665_data *haptics = dev_get_drvdata(dev);
  217. int ret = 0;
  218. mutex_lock(&haptics->input_dev->mutex);
  219. if (input_device_enabled(haptics->input_dev)) {
  220. ret = regulator_enable(haptics->regulator);
  221. if (ret) {
  222. dev_err(dev, "Failed to enable regulator\n");
  223. goto out;
  224. }
  225. ret = regmap_update_bits(haptics->regmap, DRV2665_CTRL_2,
  226. DRV2665_STANDBY, 0);
  227. if (ret) {
  228. dev_err(dev, "Failed to unset standby mode\n");
  229. regulator_disable(haptics->regulator);
  230. goto out;
  231. }
  232. }
  233. out:
  234. mutex_unlock(&haptics->input_dev->mutex);
  235. return ret;
  236. }
  237. static DEFINE_SIMPLE_DEV_PM_OPS(drv2665_pm_ops, drv2665_suspend, drv2665_resume);
  238. static const struct i2c_device_id drv2665_id[] = {
  239. { "drv2665" },
  240. { }
  241. };
  242. MODULE_DEVICE_TABLE(i2c, drv2665_id);
  243. #ifdef CONFIG_OF
  244. static const struct of_device_id drv2665_of_match[] = {
  245. { .compatible = "ti,drv2665", },
  246. { }
  247. };
  248. MODULE_DEVICE_TABLE(of, drv2665_of_match);
  249. #endif
  250. static struct i2c_driver drv2665_driver = {
  251. .probe = drv2665_probe,
  252. .driver = {
  253. .name = "drv2665-haptics",
  254. .of_match_table = of_match_ptr(drv2665_of_match),
  255. .pm = pm_sleep_ptr(&drv2665_pm_ops),
  256. },
  257. .id_table = drv2665_id,
  258. };
  259. module_i2c_driver(drv2665_driver);
  260. MODULE_DESCRIPTION("TI DRV2665 haptics driver");
  261. MODULE_LICENSE("GPL");
  262. MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");