drv260x.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * DRV260X 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/regmap.h>
  13. #include <linux/slab.h>
  14. #include <linux/delay.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <dt-bindings/input/ti-drv260x.h>
  18. #define DRV260X_STATUS 0x0
  19. #define DRV260X_MODE 0x1
  20. #define DRV260X_RT_PB_IN 0x2
  21. #define DRV260X_LIB_SEL 0x3
  22. #define DRV260X_WV_SEQ_1 0x4
  23. #define DRV260X_WV_SEQ_2 0x5
  24. #define DRV260X_WV_SEQ_3 0x6
  25. #define DRV260X_WV_SEQ_4 0x7
  26. #define DRV260X_WV_SEQ_5 0x8
  27. #define DRV260X_WV_SEQ_6 0x9
  28. #define DRV260X_WV_SEQ_7 0xa
  29. #define DRV260X_WV_SEQ_8 0xb
  30. #define DRV260X_GO 0xc
  31. #define DRV260X_OVERDRIVE_OFF 0xd
  32. #define DRV260X_SUSTAIN_P_OFF 0xe
  33. #define DRV260X_SUSTAIN_N_OFF 0xf
  34. #define DRV260X_BRAKE_OFF 0x10
  35. #define DRV260X_A_TO_V_CTRL 0x11
  36. #define DRV260X_A_TO_V_MIN_INPUT 0x12
  37. #define DRV260X_A_TO_V_MAX_INPUT 0x13
  38. #define DRV260X_A_TO_V_MIN_OUT 0x14
  39. #define DRV260X_A_TO_V_MAX_OUT 0x15
  40. #define DRV260X_RATED_VOLT 0x16
  41. #define DRV260X_OD_CLAMP_VOLT 0x17
  42. #define DRV260X_CAL_COMP 0x18
  43. #define DRV260X_CAL_BACK_EMF 0x19
  44. #define DRV260X_FEEDBACK_CTRL 0x1a
  45. #define DRV260X_CTRL1 0x1b
  46. #define DRV260X_CTRL2 0x1c
  47. #define DRV260X_CTRL3 0x1d
  48. #define DRV260X_CTRL4 0x1e
  49. #define DRV260X_CTRL5 0x1f
  50. #define DRV260X_LRA_LOOP_PERIOD 0x20
  51. #define DRV260X_VBAT_MON 0x21
  52. #define DRV260X_LRA_RES_PERIOD 0x22
  53. #define DRV260X_MAX_REG 0x23
  54. #define DRV260X_GO_BIT 0x01
  55. /* Library Selection */
  56. #define DRV260X_LIB_SEL_MASK 0x07
  57. #define DRV260X_LIB_SEL_RAM 0x0
  58. #define DRV260X_LIB_SEL_OD 0x1
  59. #define DRV260X_LIB_SEL_40_60 0x2
  60. #define DRV260X_LIB_SEL_60_80 0x3
  61. #define DRV260X_LIB_SEL_100_140 0x4
  62. #define DRV260X_LIB_SEL_140_PLUS 0x5
  63. #define DRV260X_LIB_SEL_HIZ_MASK 0x10
  64. #define DRV260X_LIB_SEL_HIZ_EN 0x01
  65. #define DRV260X_LIB_SEL_HIZ_DIS 0
  66. /* Mode register */
  67. #define DRV260X_STANDBY (1 << 6)
  68. #define DRV260X_STANDBY_MASK 0x40
  69. #define DRV260X_INTERNAL_TRIGGER 0x00
  70. #define DRV260X_EXT_TRIGGER_EDGE 0x01
  71. #define DRV260X_EXT_TRIGGER_LEVEL 0x02
  72. #define DRV260X_PWM_ANALOG_IN 0x03
  73. #define DRV260X_AUDIOHAPTIC 0x04
  74. #define DRV260X_RT_PLAYBACK 0x05
  75. #define DRV260X_DIAGNOSTICS 0x06
  76. #define DRV260X_AUTO_CAL 0x07
  77. /* Audio to Haptics Control */
  78. #define DRV260X_AUDIO_HAPTICS_PEAK_10MS (0 << 2)
  79. #define DRV260X_AUDIO_HAPTICS_PEAK_20MS (1 << 2)
  80. #define DRV260X_AUDIO_HAPTICS_PEAK_30MS (2 << 2)
  81. #define DRV260X_AUDIO_HAPTICS_PEAK_40MS (3 << 2)
  82. #define DRV260X_AUDIO_HAPTICS_FILTER_100HZ 0x00
  83. #define DRV260X_AUDIO_HAPTICS_FILTER_125HZ 0x01
  84. #define DRV260X_AUDIO_HAPTICS_FILTER_150HZ 0x02
  85. #define DRV260X_AUDIO_HAPTICS_FILTER_200HZ 0x03
  86. /* Min/Max Input/Output Voltages */
  87. #define DRV260X_AUDIO_HAPTICS_MIN_IN_VOLT 0x19
  88. #define DRV260X_AUDIO_HAPTICS_MAX_IN_VOLT 0x64
  89. #define DRV260X_AUDIO_HAPTICS_MIN_OUT_VOLT 0x19
  90. #define DRV260X_AUDIO_HAPTICS_MAX_OUT_VOLT 0xFF
  91. /* Feedback register */
  92. #define DRV260X_FB_REG_ERM_MODE 0x7f
  93. #define DRV260X_FB_REG_LRA_MODE (1 << 7)
  94. #define DRV260X_BRAKE_FACTOR_MASK 0x1f
  95. #define DRV260X_BRAKE_FACTOR_2X (1 << 0)
  96. #define DRV260X_BRAKE_FACTOR_3X (2 << 4)
  97. #define DRV260X_BRAKE_FACTOR_4X (3 << 4)
  98. #define DRV260X_BRAKE_FACTOR_6X (4 << 4)
  99. #define DRV260X_BRAKE_FACTOR_8X (5 << 4)
  100. #define DRV260X_BRAKE_FACTOR_16 (6 << 4)
  101. #define DRV260X_BRAKE_FACTOR_DIS (7 << 4)
  102. #define DRV260X_LOOP_GAIN_LOW 0xf3
  103. #define DRV260X_LOOP_GAIN_MED (1 << 2)
  104. #define DRV260X_LOOP_GAIN_HIGH (2 << 2)
  105. #define DRV260X_LOOP_GAIN_VERY_HIGH (3 << 2)
  106. #define DRV260X_BEMF_GAIN_0 0xfc
  107. #define DRV260X_BEMF_GAIN_1 (1 << 0)
  108. #define DRV260X_BEMF_GAIN_2 (2 << 0)
  109. #define DRV260X_BEMF_GAIN_3 (3 << 0)
  110. /* Control 1 register */
  111. #define DRV260X_AC_CPLE_EN (1 << 5)
  112. #define DRV260X_STARTUP_BOOST (1 << 7)
  113. /* Control 2 register */
  114. #define DRV260X_IDISS_TIME_45 0
  115. #define DRV260X_IDISS_TIME_75 (1 << 0)
  116. #define DRV260X_IDISS_TIME_150 (1 << 1)
  117. #define DRV260X_IDISS_TIME_225 0x03
  118. #define DRV260X_BLANK_TIME_45 (0 << 2)
  119. #define DRV260X_BLANK_TIME_75 (1 << 2)
  120. #define DRV260X_BLANK_TIME_150 (2 << 2)
  121. #define DRV260X_BLANK_TIME_225 (3 << 2)
  122. #define DRV260X_SAMP_TIME_150 (0 << 4)
  123. #define DRV260X_SAMP_TIME_200 (1 << 4)
  124. #define DRV260X_SAMP_TIME_250 (2 << 4)
  125. #define DRV260X_SAMP_TIME_300 (3 << 4)
  126. #define DRV260X_BRAKE_STABILIZER (1 << 6)
  127. #define DRV260X_UNIDIR_IN (0 << 7)
  128. #define DRV260X_BIDIR_IN (1 << 7)
  129. /* Control 3 Register */
  130. #define DRV260X_LRA_OPEN_LOOP (1 << 0)
  131. #define DRV260X_ANALOG_IN (1 << 1)
  132. #define DRV260X_LRA_DRV_MODE (1 << 2)
  133. #define DRV260X_RTP_UNSIGNED_DATA (1 << 3)
  134. #define DRV260X_SUPPLY_COMP_DIS (1 << 4)
  135. #define DRV260X_ERM_OPEN_LOOP (1 << 5)
  136. #define DRV260X_NG_THRESH_0 (0 << 6)
  137. #define DRV260X_NG_THRESH_2 (1 << 6)
  138. #define DRV260X_NG_THRESH_4 (2 << 6)
  139. #define DRV260X_NG_THRESH_8 (3 << 6)
  140. /* Control 4 Register */
  141. #define DRV260X_AUTOCAL_TIME_150MS (0 << 4)
  142. #define DRV260X_AUTOCAL_TIME_250MS (1 << 4)
  143. #define DRV260X_AUTOCAL_TIME_500MS (2 << 4)
  144. #define DRV260X_AUTOCAL_TIME_1000MS (3 << 4)
  145. /**
  146. * struct drv260x_data -
  147. * @input_dev: Pointer to the input device
  148. * @client: Pointer to the I2C client
  149. * @regmap: Register map of the device
  150. * @work: Work item used to off load the enable/disable of the vibration
  151. * @enable_gpio: Pointer to the gpio used for enable/disabling
  152. * @regulator: Pointer to the regulator for the IC
  153. * @magnitude: Magnitude of the vibration event
  154. * @mode: The operating mode of the IC (LRA_NO_CAL, ERM or LRA)
  155. * @library: The vibration library to be used
  156. * @rated_voltage: The rated_voltage of the actuator
  157. * @overdrive_voltage: The over drive voltage of the actuator
  158. **/
  159. struct drv260x_data {
  160. struct input_dev *input_dev;
  161. struct i2c_client *client;
  162. struct regmap *regmap;
  163. struct work_struct work;
  164. struct gpio_desc *enable_gpio;
  165. struct regulator *regulator;
  166. u8 magnitude;
  167. u32 mode;
  168. u32 library;
  169. int rated_voltage;
  170. int overdrive_voltage;
  171. };
  172. #define DRV260X_DEF_RATED_VOLT 0x90
  173. #define DRV260X_DEF_OD_CLAMP_VOLT 0x90
  174. /*
  175. * Rated and Overdriver Voltages:
  176. * Calculated using the formula r = v * 255 / 5.6
  177. * where r is what will be written to the register
  178. * and v is the rated or overdriver voltage of the actuator
  179. */
  180. static int drv260x_calculate_voltage(unsigned int voltage)
  181. {
  182. return (voltage * 255 / 5600);
  183. }
  184. static void drv260x_worker(struct work_struct *work)
  185. {
  186. struct drv260x_data *haptics = container_of(work, struct drv260x_data, work);
  187. int error;
  188. gpiod_set_value(haptics->enable_gpio, 1);
  189. /* Data sheet says to wait 250us before trying to communicate */
  190. udelay(250);
  191. error = regmap_write(haptics->regmap,
  192. DRV260X_MODE, DRV260X_RT_PLAYBACK);
  193. if (error) {
  194. dev_err(&haptics->client->dev,
  195. "Failed to write set mode: %d\n", error);
  196. } else {
  197. error = regmap_write(haptics->regmap,
  198. DRV260X_RT_PB_IN, haptics->magnitude);
  199. if (error)
  200. dev_err(&haptics->client->dev,
  201. "Failed to set magnitude: %d\n", error);
  202. }
  203. }
  204. static int drv260x_haptics_play(struct input_dev *input, void *data,
  205. struct ff_effect *effect)
  206. {
  207. struct drv260x_data *haptics = input_get_drvdata(input);
  208. haptics->mode = DRV260X_LRA_NO_CAL_MODE;
  209. /* Scale u16 magnitude into u8 register value */
  210. if (effect->u.rumble.strong_magnitude > 0)
  211. haptics->magnitude = effect->u.rumble.strong_magnitude >> 8;
  212. else if (effect->u.rumble.weak_magnitude > 0)
  213. haptics->magnitude = effect->u.rumble.weak_magnitude >> 8;
  214. else
  215. haptics->magnitude = 0;
  216. schedule_work(&haptics->work);
  217. return 0;
  218. }
  219. static void drv260x_close(struct input_dev *input)
  220. {
  221. struct drv260x_data *haptics = input_get_drvdata(input);
  222. int error;
  223. cancel_work_sync(&haptics->work);
  224. error = regmap_write(haptics->regmap, DRV260X_MODE, DRV260X_STANDBY);
  225. if (error)
  226. dev_err(&haptics->client->dev,
  227. "Failed to enter standby mode: %d\n", error);
  228. gpiod_set_value(haptics->enable_gpio, 0);
  229. }
  230. static const struct reg_sequence drv260x_lra_cal_regs[] = {
  231. { DRV260X_MODE, DRV260X_AUTO_CAL },
  232. { DRV260X_CTRL3, DRV260X_NG_THRESH_2 | DRV260X_RTP_UNSIGNED_DATA },
  233. { DRV260X_FEEDBACK_CTRL, DRV260X_FB_REG_LRA_MODE |
  234. DRV260X_BRAKE_FACTOR_4X | DRV260X_LOOP_GAIN_HIGH },
  235. };
  236. static const struct reg_sequence drv260x_lra_init_regs[] = {
  237. { DRV260X_MODE, DRV260X_RT_PLAYBACK },
  238. { DRV260X_A_TO_V_CTRL, DRV260X_AUDIO_HAPTICS_PEAK_20MS |
  239. DRV260X_AUDIO_HAPTICS_FILTER_125HZ },
  240. { DRV260X_A_TO_V_MIN_INPUT, DRV260X_AUDIO_HAPTICS_MIN_IN_VOLT },
  241. { DRV260X_A_TO_V_MAX_INPUT, DRV260X_AUDIO_HAPTICS_MAX_IN_VOLT },
  242. { DRV260X_A_TO_V_MIN_OUT, DRV260X_AUDIO_HAPTICS_MIN_OUT_VOLT },
  243. { DRV260X_A_TO_V_MAX_OUT, DRV260X_AUDIO_HAPTICS_MAX_OUT_VOLT },
  244. { DRV260X_FEEDBACK_CTRL, DRV260X_FB_REG_LRA_MODE |
  245. DRV260X_BRAKE_FACTOR_2X | DRV260X_LOOP_GAIN_MED |
  246. DRV260X_BEMF_GAIN_3 },
  247. { DRV260X_CTRL1, DRV260X_STARTUP_BOOST },
  248. { DRV260X_CTRL2, DRV260X_SAMP_TIME_250 },
  249. { DRV260X_CTRL3, DRV260X_NG_THRESH_2 | DRV260X_RTP_UNSIGNED_DATA | DRV260X_ANALOG_IN },
  250. { DRV260X_CTRL4, DRV260X_AUTOCAL_TIME_500MS },
  251. };
  252. static const struct reg_sequence drv260x_erm_cal_regs[] = {
  253. { DRV260X_MODE, DRV260X_AUTO_CAL },
  254. { DRV260X_A_TO_V_MIN_INPUT, DRV260X_AUDIO_HAPTICS_MIN_IN_VOLT },
  255. { DRV260X_A_TO_V_MAX_INPUT, DRV260X_AUDIO_HAPTICS_MAX_IN_VOLT },
  256. { DRV260X_A_TO_V_MIN_OUT, DRV260X_AUDIO_HAPTICS_MIN_OUT_VOLT },
  257. { DRV260X_A_TO_V_MAX_OUT, DRV260X_AUDIO_HAPTICS_MAX_OUT_VOLT },
  258. { DRV260X_FEEDBACK_CTRL, DRV260X_BRAKE_FACTOR_3X |
  259. DRV260X_LOOP_GAIN_MED | DRV260X_BEMF_GAIN_2 },
  260. { DRV260X_CTRL1, DRV260X_STARTUP_BOOST },
  261. { DRV260X_CTRL2, DRV260X_SAMP_TIME_250 | DRV260X_BLANK_TIME_75 |
  262. DRV260X_IDISS_TIME_75 },
  263. { DRV260X_CTRL3, DRV260X_NG_THRESH_2 | DRV260X_RTP_UNSIGNED_DATA },
  264. { DRV260X_CTRL4, DRV260X_AUTOCAL_TIME_500MS },
  265. };
  266. static int drv260x_init(struct drv260x_data *haptics)
  267. {
  268. int error;
  269. unsigned int cal_buf;
  270. error = regmap_write(haptics->regmap,
  271. DRV260X_RATED_VOLT, haptics->rated_voltage);
  272. if (error) {
  273. dev_err(&haptics->client->dev,
  274. "Failed to write DRV260X_RATED_VOLT register: %d\n",
  275. error);
  276. return error;
  277. }
  278. error = regmap_write(haptics->regmap,
  279. DRV260X_OD_CLAMP_VOLT, haptics->overdrive_voltage);
  280. if (error) {
  281. dev_err(&haptics->client->dev,
  282. "Failed to write DRV260X_OD_CLAMP_VOLT register: %d\n",
  283. error);
  284. return error;
  285. }
  286. switch (haptics->mode) {
  287. case DRV260X_LRA_MODE:
  288. error = regmap_register_patch(haptics->regmap,
  289. drv260x_lra_cal_regs,
  290. ARRAY_SIZE(drv260x_lra_cal_regs));
  291. if (error) {
  292. dev_err(&haptics->client->dev,
  293. "Failed to write LRA calibration registers: %d\n",
  294. error);
  295. return error;
  296. }
  297. break;
  298. case DRV260X_ERM_MODE:
  299. error = regmap_register_patch(haptics->regmap,
  300. drv260x_erm_cal_regs,
  301. ARRAY_SIZE(drv260x_erm_cal_regs));
  302. if (error) {
  303. dev_err(&haptics->client->dev,
  304. "Failed to write ERM calibration registers: %d\n",
  305. error);
  306. return error;
  307. }
  308. error = regmap_update_bits(haptics->regmap, DRV260X_LIB_SEL,
  309. DRV260X_LIB_SEL_MASK,
  310. haptics->library);
  311. if (error) {
  312. dev_err(&haptics->client->dev,
  313. "Failed to write DRV260X_LIB_SEL register: %d\n",
  314. error);
  315. return error;
  316. }
  317. break;
  318. default:
  319. error = regmap_register_patch(haptics->regmap,
  320. drv260x_lra_init_regs,
  321. ARRAY_SIZE(drv260x_lra_init_regs));
  322. if (error) {
  323. dev_err(&haptics->client->dev,
  324. "Failed to write LRA init registers: %d\n",
  325. error);
  326. return error;
  327. }
  328. error = regmap_update_bits(haptics->regmap, DRV260X_LIB_SEL,
  329. DRV260X_LIB_SEL_MASK,
  330. haptics->library);
  331. if (error) {
  332. dev_err(&haptics->client->dev,
  333. "Failed to write DRV260X_LIB_SEL register: %d\n",
  334. error);
  335. return error;
  336. }
  337. /* No need to set GO bit here */
  338. return 0;
  339. }
  340. error = regmap_write(haptics->regmap, DRV260X_GO, DRV260X_GO_BIT);
  341. if (error) {
  342. dev_err(&haptics->client->dev,
  343. "Failed to write GO register: %d\n",
  344. error);
  345. return error;
  346. }
  347. do {
  348. usleep_range(15000, 15500);
  349. error = regmap_read(haptics->regmap, DRV260X_GO, &cal_buf);
  350. if (error) {
  351. dev_err(&haptics->client->dev,
  352. "Failed to read GO register: %d\n",
  353. error);
  354. return error;
  355. }
  356. } while (cal_buf == DRV260X_GO_BIT);
  357. return 0;
  358. }
  359. static const struct regmap_config drv260x_regmap_config = {
  360. .reg_bits = 8,
  361. .val_bits = 8,
  362. .max_register = DRV260X_MAX_REG,
  363. .cache_type = REGCACHE_NONE,
  364. };
  365. static int drv260x_probe(struct i2c_client *client)
  366. {
  367. struct device *dev = &client->dev;
  368. struct drv260x_data *haptics;
  369. u32 voltage;
  370. int error;
  371. haptics = devm_kzalloc(dev, sizeof(*haptics), GFP_KERNEL);
  372. if (!haptics)
  373. return -ENOMEM;
  374. error = device_property_read_u32(dev, "mode", &haptics->mode);
  375. if (error) {
  376. dev_err(dev, "Can't fetch 'mode' property: %d\n", error);
  377. return error;
  378. }
  379. if (haptics->mode < DRV260X_LRA_MODE ||
  380. haptics->mode > DRV260X_ERM_MODE) {
  381. dev_err(dev, "Vibrator mode is invalid: %i\n", haptics->mode);
  382. return -EINVAL;
  383. }
  384. error = device_property_read_u32(dev, "library-sel", &haptics->library);
  385. if (error) {
  386. dev_err(dev, "Can't fetch 'library-sel' property: %d\n", error);
  387. return error;
  388. }
  389. if (haptics->library < DRV260X_LIB_EMPTY ||
  390. haptics->library > DRV260X_ERM_LIB_F) {
  391. dev_err(dev,
  392. "Library value is invalid: %i\n", haptics->library);
  393. return -EINVAL;
  394. }
  395. if (haptics->mode == DRV260X_LRA_MODE &&
  396. haptics->library != DRV260X_LIB_EMPTY &&
  397. haptics->library != DRV260X_LIB_LRA) {
  398. dev_err(dev, "LRA Mode with ERM Library mismatch\n");
  399. return -EINVAL;
  400. }
  401. if (haptics->mode == DRV260X_ERM_MODE &&
  402. (haptics->library == DRV260X_LIB_EMPTY ||
  403. haptics->library == DRV260X_LIB_LRA)) {
  404. dev_err(dev, "ERM Mode with LRA Library mismatch\n");
  405. return -EINVAL;
  406. }
  407. error = device_property_read_u32(dev, "vib-rated-mv", &voltage);
  408. haptics->rated_voltage = error ? DRV260X_DEF_RATED_VOLT :
  409. drv260x_calculate_voltage(voltage);
  410. error = device_property_read_u32(dev, "vib-overdrive-mv", &voltage);
  411. haptics->overdrive_voltage = error ? DRV260X_DEF_OD_CLAMP_VOLT :
  412. drv260x_calculate_voltage(voltage);
  413. haptics->regulator = devm_regulator_get(dev, "vbat");
  414. if (IS_ERR(haptics->regulator)) {
  415. error = PTR_ERR(haptics->regulator);
  416. dev_err(dev, "unable to get regulator, error: %d\n", error);
  417. return error;
  418. }
  419. haptics->enable_gpio = devm_gpiod_get_optional(dev, "enable",
  420. GPIOD_OUT_HIGH);
  421. if (IS_ERR(haptics->enable_gpio))
  422. return PTR_ERR(haptics->enable_gpio);
  423. haptics->input_dev = devm_input_allocate_device(dev);
  424. if (!haptics->input_dev) {
  425. dev_err(dev, "Failed to allocate input device\n");
  426. return -ENOMEM;
  427. }
  428. haptics->input_dev->name = "drv260x:haptics";
  429. haptics->input_dev->close = drv260x_close;
  430. input_set_drvdata(haptics->input_dev, haptics);
  431. input_set_capability(haptics->input_dev, EV_FF, FF_RUMBLE);
  432. error = input_ff_create_memless(haptics->input_dev, NULL,
  433. drv260x_haptics_play);
  434. if (error) {
  435. dev_err(dev, "input_ff_create() failed: %d\n", error);
  436. return error;
  437. }
  438. INIT_WORK(&haptics->work, drv260x_worker);
  439. haptics->client = client;
  440. i2c_set_clientdata(client, haptics);
  441. haptics->regmap = devm_regmap_init_i2c(client, &drv260x_regmap_config);
  442. if (IS_ERR(haptics->regmap)) {
  443. error = PTR_ERR(haptics->regmap);
  444. dev_err(dev, "Failed to allocate register map: %d\n", error);
  445. return error;
  446. }
  447. error = drv260x_init(haptics);
  448. if (error) {
  449. dev_err(dev, "Device init failed: %d\n", error);
  450. return error;
  451. }
  452. error = input_register_device(haptics->input_dev);
  453. if (error) {
  454. dev_err(dev, "couldn't register input device: %d\n", error);
  455. return error;
  456. }
  457. return 0;
  458. }
  459. static int drv260x_suspend(struct device *dev)
  460. {
  461. struct drv260x_data *haptics = dev_get_drvdata(dev);
  462. int error;
  463. guard(mutex)(&haptics->input_dev->mutex);
  464. if (input_device_enabled(haptics->input_dev)) {
  465. error = regmap_update_bits(haptics->regmap,
  466. DRV260X_MODE,
  467. DRV260X_STANDBY_MASK,
  468. DRV260X_STANDBY);
  469. if (error) {
  470. dev_err(dev, "Failed to set standby mode\n");
  471. return error;
  472. }
  473. gpiod_set_value(haptics->enable_gpio, 0);
  474. error = regulator_disable(haptics->regulator);
  475. if (error) {
  476. dev_err(dev, "Failed to disable regulator\n");
  477. regmap_update_bits(haptics->regmap,
  478. DRV260X_MODE,
  479. DRV260X_STANDBY_MASK, 0);
  480. return error;
  481. }
  482. }
  483. return 0;
  484. }
  485. static int drv260x_resume(struct device *dev)
  486. {
  487. struct drv260x_data *haptics = dev_get_drvdata(dev);
  488. int error;
  489. guard(mutex)(&haptics->input_dev->mutex);
  490. if (input_device_enabled(haptics->input_dev)) {
  491. error = regulator_enable(haptics->regulator);
  492. if (error) {
  493. dev_err(dev, "Failed to enable regulator\n");
  494. return error;
  495. }
  496. error = regmap_update_bits(haptics->regmap,
  497. DRV260X_MODE,
  498. DRV260X_STANDBY_MASK, 0);
  499. if (error) {
  500. dev_err(dev, "Failed to unset standby mode\n");
  501. regulator_disable(haptics->regulator);
  502. return error;
  503. }
  504. gpiod_set_value(haptics->enable_gpio, 1);
  505. }
  506. return 0;
  507. }
  508. static DEFINE_SIMPLE_DEV_PM_OPS(drv260x_pm_ops, drv260x_suspend, drv260x_resume);
  509. static const struct i2c_device_id drv260x_id[] = {
  510. { "drv2605l" },
  511. { }
  512. };
  513. MODULE_DEVICE_TABLE(i2c, drv260x_id);
  514. static const struct of_device_id drv260x_of_match[] = {
  515. { .compatible = "ti,drv2604", },
  516. { .compatible = "ti,drv2604l", },
  517. { .compatible = "ti,drv2605", },
  518. { .compatible = "ti,drv2605l", },
  519. { }
  520. };
  521. MODULE_DEVICE_TABLE(of, drv260x_of_match);
  522. static struct i2c_driver drv260x_driver = {
  523. .probe = drv260x_probe,
  524. .driver = {
  525. .name = "drv260x-haptics",
  526. .of_match_table = drv260x_of_match,
  527. .pm = pm_sleep_ptr(&drv260x_pm_ops),
  528. },
  529. .id_table = drv260x_id,
  530. };
  531. module_i2c_driver(drv260x_driver);
  532. MODULE_DESCRIPTION("TI DRV260x haptics driver");
  533. MODULE_LICENSE("GPL");
  534. MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");