drv260x.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * DRV260X haptics driver family
  4. *
  5. * Author: Dan Murphy <[email protected]>
  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_ANANLOG_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. u32 magnitude;
  167. u32 mode;
  168. u32 library;
  169. int rated_voltage;
  170. int overdrive_voltage;
  171. };
  172. static const struct reg_default drv260x_reg_defs[] = {
  173. { DRV260X_STATUS, 0xe0 },
  174. { DRV260X_MODE, 0x40 },
  175. { DRV260X_RT_PB_IN, 0x00 },
  176. { DRV260X_LIB_SEL, 0x00 },
  177. { DRV260X_WV_SEQ_1, 0x01 },
  178. { DRV260X_WV_SEQ_2, 0x00 },
  179. { DRV260X_WV_SEQ_3, 0x00 },
  180. { DRV260X_WV_SEQ_4, 0x00 },
  181. { DRV260X_WV_SEQ_5, 0x00 },
  182. { DRV260X_WV_SEQ_6, 0x00 },
  183. { DRV260X_WV_SEQ_7, 0x00 },
  184. { DRV260X_WV_SEQ_8, 0x00 },
  185. { DRV260X_GO, 0x00 },
  186. { DRV260X_OVERDRIVE_OFF, 0x00 },
  187. { DRV260X_SUSTAIN_P_OFF, 0x00 },
  188. { DRV260X_SUSTAIN_N_OFF, 0x00 },
  189. { DRV260X_BRAKE_OFF, 0x00 },
  190. { DRV260X_A_TO_V_CTRL, 0x05 },
  191. { DRV260X_A_TO_V_MIN_INPUT, 0x19 },
  192. { DRV260X_A_TO_V_MAX_INPUT, 0xff },
  193. { DRV260X_A_TO_V_MIN_OUT, 0x19 },
  194. { DRV260X_A_TO_V_MAX_OUT, 0xff },
  195. { DRV260X_RATED_VOLT, 0x3e },
  196. { DRV260X_OD_CLAMP_VOLT, 0x8c },
  197. { DRV260X_CAL_COMP, 0x0c },
  198. { DRV260X_CAL_BACK_EMF, 0x6c },
  199. { DRV260X_FEEDBACK_CTRL, 0x36 },
  200. { DRV260X_CTRL1, 0x93 },
  201. { DRV260X_CTRL2, 0xfa },
  202. { DRV260X_CTRL3, 0xa0 },
  203. { DRV260X_CTRL4, 0x20 },
  204. { DRV260X_CTRL5, 0x80 },
  205. { DRV260X_LRA_LOOP_PERIOD, 0x33 },
  206. { DRV260X_VBAT_MON, 0x00 },
  207. { DRV260X_LRA_RES_PERIOD, 0x00 },
  208. };
  209. #define DRV260X_DEF_RATED_VOLT 0x90
  210. #define DRV260X_DEF_OD_CLAMP_VOLT 0x90
  211. /*
  212. * Rated and Overdriver Voltages:
  213. * Calculated using the formula r = v * 255 / 5.6
  214. * where r is what will be written to the register
  215. * and v is the rated or overdriver voltage of the actuator
  216. */
  217. static int drv260x_calculate_voltage(unsigned int voltage)
  218. {
  219. return (voltage * 255 / 5600);
  220. }
  221. static void drv260x_worker(struct work_struct *work)
  222. {
  223. struct drv260x_data *haptics = container_of(work, struct drv260x_data, work);
  224. int error;
  225. gpiod_set_value(haptics->enable_gpio, 1);
  226. /* Data sheet says to wait 250us before trying to communicate */
  227. udelay(250);
  228. error = regmap_write(haptics->regmap,
  229. DRV260X_MODE, DRV260X_RT_PLAYBACK);
  230. if (error) {
  231. dev_err(&haptics->client->dev,
  232. "Failed to write set mode: %d\n", error);
  233. } else {
  234. error = regmap_write(haptics->regmap,
  235. DRV260X_RT_PB_IN, haptics->magnitude);
  236. if (error)
  237. dev_err(&haptics->client->dev,
  238. "Failed to set magnitude: %d\n", error);
  239. }
  240. }
  241. static int drv260x_haptics_play(struct input_dev *input, void *data,
  242. struct ff_effect *effect)
  243. {
  244. struct drv260x_data *haptics = input_get_drvdata(input);
  245. haptics->mode = DRV260X_LRA_NO_CAL_MODE;
  246. if (effect->u.rumble.strong_magnitude > 0)
  247. haptics->magnitude = effect->u.rumble.strong_magnitude;
  248. else if (effect->u.rumble.weak_magnitude > 0)
  249. haptics->magnitude = effect->u.rumble.weak_magnitude;
  250. else
  251. haptics->magnitude = 0;
  252. schedule_work(&haptics->work);
  253. return 0;
  254. }
  255. static void drv260x_close(struct input_dev *input)
  256. {
  257. struct drv260x_data *haptics = input_get_drvdata(input);
  258. int error;
  259. cancel_work_sync(&haptics->work);
  260. error = regmap_write(haptics->regmap, DRV260X_MODE, DRV260X_STANDBY);
  261. if (error)
  262. dev_err(&haptics->client->dev,
  263. "Failed to enter standby mode: %d\n", error);
  264. gpiod_set_value(haptics->enable_gpio, 0);
  265. }
  266. static const struct reg_sequence drv260x_lra_cal_regs[] = {
  267. { DRV260X_MODE, DRV260X_AUTO_CAL },
  268. { DRV260X_CTRL3, DRV260X_NG_THRESH_2 },
  269. { DRV260X_FEEDBACK_CTRL, DRV260X_FB_REG_LRA_MODE |
  270. DRV260X_BRAKE_FACTOR_4X | DRV260X_LOOP_GAIN_HIGH },
  271. };
  272. static const struct reg_sequence drv260x_lra_init_regs[] = {
  273. { DRV260X_MODE, DRV260X_RT_PLAYBACK },
  274. { DRV260X_A_TO_V_CTRL, DRV260X_AUDIO_HAPTICS_PEAK_20MS |
  275. DRV260X_AUDIO_HAPTICS_FILTER_125HZ },
  276. { DRV260X_A_TO_V_MIN_INPUT, DRV260X_AUDIO_HAPTICS_MIN_IN_VOLT },
  277. { DRV260X_A_TO_V_MAX_INPUT, DRV260X_AUDIO_HAPTICS_MAX_IN_VOLT },
  278. { DRV260X_A_TO_V_MIN_OUT, DRV260X_AUDIO_HAPTICS_MIN_OUT_VOLT },
  279. { DRV260X_A_TO_V_MAX_OUT, DRV260X_AUDIO_HAPTICS_MAX_OUT_VOLT },
  280. { DRV260X_FEEDBACK_CTRL, DRV260X_FB_REG_LRA_MODE |
  281. DRV260X_BRAKE_FACTOR_2X | DRV260X_LOOP_GAIN_MED |
  282. DRV260X_BEMF_GAIN_3 },
  283. { DRV260X_CTRL1, DRV260X_STARTUP_BOOST },
  284. { DRV260X_CTRL2, DRV260X_SAMP_TIME_250 },
  285. { DRV260X_CTRL3, DRV260X_NG_THRESH_2 | DRV260X_ANANLOG_IN },
  286. { DRV260X_CTRL4, DRV260X_AUTOCAL_TIME_500MS },
  287. };
  288. static const struct reg_sequence drv260x_erm_cal_regs[] = {
  289. { DRV260X_MODE, DRV260X_AUTO_CAL },
  290. { DRV260X_A_TO_V_MIN_INPUT, DRV260X_AUDIO_HAPTICS_MIN_IN_VOLT },
  291. { DRV260X_A_TO_V_MAX_INPUT, DRV260X_AUDIO_HAPTICS_MAX_IN_VOLT },
  292. { DRV260X_A_TO_V_MIN_OUT, DRV260X_AUDIO_HAPTICS_MIN_OUT_VOLT },
  293. { DRV260X_A_TO_V_MAX_OUT, DRV260X_AUDIO_HAPTICS_MAX_OUT_VOLT },
  294. { DRV260X_FEEDBACK_CTRL, DRV260X_BRAKE_FACTOR_3X |
  295. DRV260X_LOOP_GAIN_MED | DRV260X_BEMF_GAIN_2 },
  296. { DRV260X_CTRL1, DRV260X_STARTUP_BOOST },
  297. { DRV260X_CTRL2, DRV260X_SAMP_TIME_250 | DRV260X_BLANK_TIME_75 |
  298. DRV260X_IDISS_TIME_75 },
  299. { DRV260X_CTRL3, DRV260X_NG_THRESH_2 | DRV260X_ERM_OPEN_LOOP },
  300. { DRV260X_CTRL4, DRV260X_AUTOCAL_TIME_500MS },
  301. };
  302. static int drv260x_init(struct drv260x_data *haptics)
  303. {
  304. int error;
  305. unsigned int cal_buf;
  306. error = regmap_write(haptics->regmap,
  307. DRV260X_RATED_VOLT, haptics->rated_voltage);
  308. if (error) {
  309. dev_err(&haptics->client->dev,
  310. "Failed to write DRV260X_RATED_VOLT register: %d\n",
  311. error);
  312. return error;
  313. }
  314. error = regmap_write(haptics->regmap,
  315. DRV260X_OD_CLAMP_VOLT, haptics->overdrive_voltage);
  316. if (error) {
  317. dev_err(&haptics->client->dev,
  318. "Failed to write DRV260X_OD_CLAMP_VOLT register: %d\n",
  319. error);
  320. return error;
  321. }
  322. switch (haptics->mode) {
  323. case DRV260X_LRA_MODE:
  324. error = regmap_register_patch(haptics->regmap,
  325. drv260x_lra_cal_regs,
  326. ARRAY_SIZE(drv260x_lra_cal_regs));
  327. if (error) {
  328. dev_err(&haptics->client->dev,
  329. "Failed to write LRA calibration registers: %d\n",
  330. error);
  331. return error;
  332. }
  333. break;
  334. case DRV260X_ERM_MODE:
  335. error = regmap_register_patch(haptics->regmap,
  336. drv260x_erm_cal_regs,
  337. ARRAY_SIZE(drv260x_erm_cal_regs));
  338. if (error) {
  339. dev_err(&haptics->client->dev,
  340. "Failed to write ERM calibration registers: %d\n",
  341. error);
  342. return error;
  343. }
  344. error = regmap_update_bits(haptics->regmap, DRV260X_LIB_SEL,
  345. DRV260X_LIB_SEL_MASK,
  346. haptics->library);
  347. if (error) {
  348. dev_err(&haptics->client->dev,
  349. "Failed to write DRV260X_LIB_SEL register: %d\n",
  350. error);
  351. return error;
  352. }
  353. break;
  354. default:
  355. error = regmap_register_patch(haptics->regmap,
  356. drv260x_lra_init_regs,
  357. ARRAY_SIZE(drv260x_lra_init_regs));
  358. if (error) {
  359. dev_err(&haptics->client->dev,
  360. "Failed to write LRA init registers: %d\n",
  361. error);
  362. return error;
  363. }
  364. error = regmap_update_bits(haptics->regmap, DRV260X_LIB_SEL,
  365. DRV260X_LIB_SEL_MASK,
  366. haptics->library);
  367. if (error) {
  368. dev_err(&haptics->client->dev,
  369. "Failed to write DRV260X_LIB_SEL register: %d\n",
  370. error);
  371. return error;
  372. }
  373. /* No need to set GO bit here */
  374. return 0;
  375. }
  376. error = regmap_write(haptics->regmap, DRV260X_GO, DRV260X_GO_BIT);
  377. if (error) {
  378. dev_err(&haptics->client->dev,
  379. "Failed to write GO register: %d\n",
  380. error);
  381. return error;
  382. }
  383. do {
  384. usleep_range(15000, 15500);
  385. error = regmap_read(haptics->regmap, DRV260X_GO, &cal_buf);
  386. if (error) {
  387. dev_err(&haptics->client->dev,
  388. "Failed to read GO register: %d\n",
  389. error);
  390. return error;
  391. }
  392. } while (cal_buf == DRV260X_GO_BIT);
  393. return 0;
  394. }
  395. static const struct regmap_config drv260x_regmap_config = {
  396. .reg_bits = 8,
  397. .val_bits = 8,
  398. .max_register = DRV260X_MAX_REG,
  399. .reg_defaults = drv260x_reg_defs,
  400. .num_reg_defaults = ARRAY_SIZE(drv260x_reg_defs),
  401. .cache_type = REGCACHE_NONE,
  402. };
  403. static int drv260x_probe(struct i2c_client *client,
  404. const struct i2c_device_id *id)
  405. {
  406. struct device *dev = &client->dev;
  407. struct drv260x_data *haptics;
  408. u32 voltage;
  409. int error;
  410. haptics = devm_kzalloc(dev, sizeof(*haptics), GFP_KERNEL);
  411. if (!haptics)
  412. return -ENOMEM;
  413. error = device_property_read_u32(dev, "mode", &haptics->mode);
  414. if (error) {
  415. dev_err(dev, "Can't fetch 'mode' property: %d\n", error);
  416. return error;
  417. }
  418. if (haptics->mode < DRV260X_LRA_MODE ||
  419. haptics->mode > DRV260X_ERM_MODE) {
  420. dev_err(dev, "Vibrator mode is invalid: %i\n", haptics->mode);
  421. return -EINVAL;
  422. }
  423. error = device_property_read_u32(dev, "library-sel", &haptics->library);
  424. if (error) {
  425. dev_err(dev, "Can't fetch 'library-sel' property: %d\n", error);
  426. return error;
  427. }
  428. if (haptics->library < DRV260X_LIB_EMPTY ||
  429. haptics->library > DRV260X_ERM_LIB_F) {
  430. dev_err(dev,
  431. "Library value is invalid: %i\n", haptics->library);
  432. return -EINVAL;
  433. }
  434. if (haptics->mode == DRV260X_LRA_MODE &&
  435. haptics->library != DRV260X_LIB_EMPTY &&
  436. haptics->library != DRV260X_LIB_LRA) {
  437. dev_err(dev, "LRA Mode with ERM Library mismatch\n");
  438. return -EINVAL;
  439. }
  440. if (haptics->mode == DRV260X_ERM_MODE &&
  441. (haptics->library == DRV260X_LIB_EMPTY ||
  442. haptics->library == DRV260X_LIB_LRA)) {
  443. dev_err(dev, "ERM Mode with LRA Library mismatch\n");
  444. return -EINVAL;
  445. }
  446. error = device_property_read_u32(dev, "vib-rated-mv", &voltage);
  447. haptics->rated_voltage = error ? DRV260X_DEF_RATED_VOLT :
  448. drv260x_calculate_voltage(voltage);
  449. error = device_property_read_u32(dev, "vib-overdrive-mv", &voltage);
  450. haptics->overdrive_voltage = error ? DRV260X_DEF_OD_CLAMP_VOLT :
  451. drv260x_calculate_voltage(voltage);
  452. haptics->regulator = devm_regulator_get(dev, "vbat");
  453. if (IS_ERR(haptics->regulator)) {
  454. error = PTR_ERR(haptics->regulator);
  455. dev_err(dev, "unable to get regulator, error: %d\n", error);
  456. return error;
  457. }
  458. haptics->enable_gpio = devm_gpiod_get_optional(dev, "enable",
  459. GPIOD_OUT_HIGH);
  460. if (IS_ERR(haptics->enable_gpio))
  461. return PTR_ERR(haptics->enable_gpio);
  462. haptics->input_dev = devm_input_allocate_device(dev);
  463. if (!haptics->input_dev) {
  464. dev_err(dev, "Failed to allocate input device\n");
  465. return -ENOMEM;
  466. }
  467. haptics->input_dev->name = "drv260x:haptics";
  468. haptics->input_dev->close = drv260x_close;
  469. input_set_drvdata(haptics->input_dev, haptics);
  470. input_set_capability(haptics->input_dev, EV_FF, FF_RUMBLE);
  471. error = input_ff_create_memless(haptics->input_dev, NULL,
  472. drv260x_haptics_play);
  473. if (error) {
  474. dev_err(dev, "input_ff_create() failed: %d\n", error);
  475. return error;
  476. }
  477. INIT_WORK(&haptics->work, drv260x_worker);
  478. haptics->client = client;
  479. i2c_set_clientdata(client, haptics);
  480. haptics->regmap = devm_regmap_init_i2c(client, &drv260x_regmap_config);
  481. if (IS_ERR(haptics->regmap)) {
  482. error = PTR_ERR(haptics->regmap);
  483. dev_err(dev, "Failed to allocate register map: %d\n", error);
  484. return error;
  485. }
  486. error = drv260x_init(haptics);
  487. if (error) {
  488. dev_err(dev, "Device init failed: %d\n", error);
  489. return error;
  490. }
  491. error = input_register_device(haptics->input_dev);
  492. if (error) {
  493. dev_err(dev, "couldn't register input device: %d\n", error);
  494. return error;
  495. }
  496. return 0;
  497. }
  498. static int __maybe_unused drv260x_suspend(struct device *dev)
  499. {
  500. struct drv260x_data *haptics = dev_get_drvdata(dev);
  501. int ret = 0;
  502. mutex_lock(&haptics->input_dev->mutex);
  503. if (input_device_enabled(haptics->input_dev)) {
  504. ret = regmap_update_bits(haptics->regmap,
  505. DRV260X_MODE,
  506. DRV260X_STANDBY_MASK,
  507. DRV260X_STANDBY);
  508. if (ret) {
  509. dev_err(dev, "Failed to set standby mode\n");
  510. goto out;
  511. }
  512. gpiod_set_value(haptics->enable_gpio, 0);
  513. ret = regulator_disable(haptics->regulator);
  514. if (ret) {
  515. dev_err(dev, "Failed to disable regulator\n");
  516. regmap_update_bits(haptics->regmap,
  517. DRV260X_MODE,
  518. DRV260X_STANDBY_MASK, 0);
  519. }
  520. }
  521. out:
  522. mutex_unlock(&haptics->input_dev->mutex);
  523. return ret;
  524. }
  525. static int __maybe_unused drv260x_resume(struct device *dev)
  526. {
  527. struct drv260x_data *haptics = dev_get_drvdata(dev);
  528. int ret = 0;
  529. mutex_lock(&haptics->input_dev->mutex);
  530. if (input_device_enabled(haptics->input_dev)) {
  531. ret = regulator_enable(haptics->regulator);
  532. if (ret) {
  533. dev_err(dev, "Failed to enable regulator\n");
  534. goto out;
  535. }
  536. ret = regmap_update_bits(haptics->regmap,
  537. DRV260X_MODE,
  538. DRV260X_STANDBY_MASK, 0);
  539. if (ret) {
  540. dev_err(dev, "Failed to unset standby mode\n");
  541. regulator_disable(haptics->regulator);
  542. goto out;
  543. }
  544. gpiod_set_value(haptics->enable_gpio, 1);
  545. }
  546. out:
  547. mutex_unlock(&haptics->input_dev->mutex);
  548. return ret;
  549. }
  550. static SIMPLE_DEV_PM_OPS(drv260x_pm_ops, drv260x_suspend, drv260x_resume);
  551. static const struct i2c_device_id drv260x_id[] = {
  552. { "drv2605l", 0 },
  553. { }
  554. };
  555. MODULE_DEVICE_TABLE(i2c, drv260x_id);
  556. static const struct of_device_id drv260x_of_match[] = {
  557. { .compatible = "ti,drv2604", },
  558. { .compatible = "ti,drv2604l", },
  559. { .compatible = "ti,drv2605", },
  560. { .compatible = "ti,drv2605l", },
  561. { }
  562. };
  563. MODULE_DEVICE_TABLE(of, drv260x_of_match);
  564. static struct i2c_driver drv260x_driver = {
  565. .probe = drv260x_probe,
  566. .driver = {
  567. .name = "drv260x-haptics",
  568. .of_match_table = drv260x_of_match,
  569. .pm = &drv260x_pm_ops,
  570. },
  571. .id_table = drv260x_id,
  572. };
  573. module_i2c_driver(drv260x_driver);
  574. MODULE_DESCRIPTION("TI DRV260x haptics driver");
  575. MODULE_LICENSE("GPL");
  576. MODULE_AUTHOR("Dan Murphy <[email protected]>");