drv260x.c 20 KB

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