isa1200.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /*
  2. * isa1200.c - Haptic Motor
  3. *
  4. * Copyright (C) 2009 Samsung Electronics
  5. * Kyungmin Park <kyungmin.park@samsung.com>
  6. * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
  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. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/i2c.h>
  15. #include <linux/gpio.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/pwm.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/slab.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/clk.h>
  23. #include <linux/i2c/isa1200.h>
  24. #include "../staging/android/timed_output.h"
  25. #include <linux/of_gpio.h>
  26. #define ISA1200_HCTRL0 0x30
  27. #define ISA1200_HCTRL1 0x31
  28. #define ISA1200_HCTRL5 0x35
  29. #define ISA1200_HCTRL0_RESET 0x01
  30. #define ISA1200_HCTRL1_RESET 0x4B
  31. #define ISA1200_HCTRL5_VIB_STRT 0xD5
  32. #define ISA1200_HCTRL5_VIB_STOP 0x6B
  33. #define ISA1200_POWER_DOWN_MASK 0x7F
  34. struct isa1200_chip {
  35. struct i2c_client *client;
  36. struct isa1200_platform_data *pdata;
  37. struct pwm_device *pwm;
  38. struct hrtimer timer;
  39. struct timed_output_dev dev;
  40. struct work_struct work;
  41. struct mutex lock;
  42. struct mutex lock_clk;
  43. unsigned int enable;
  44. unsigned int period_ns;
  45. bool is_len_gpio_valid;
  46. struct regulator **regs;
  47. bool clk_on;
  48. u8 hctrl0_val;
  49. struct clk *pwm_clk;
  50. };
  51. static int isa1200_read_reg(struct i2c_client *client, int reg)
  52. {
  53. int ret;
  54. ret = i2c_smbus_read_byte_data(client, reg);
  55. if (ret < 0)
  56. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  57. return ret;
  58. }
  59. static int isa1200_write_reg(struct i2c_client *client, int reg, u8 value)
  60. {
  61. int ret;
  62. ret = i2c_smbus_write_byte_data(client, reg, value);
  63. if (ret < 0)
  64. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  65. return ret;
  66. }
  67. static void isa1200_vib_set(struct isa1200_chip *haptic, int enable)
  68. {
  69. int rc = 0;
  70. if (enable) {
  71. /* if hen and len are seperate then enable hen
  72. * otherwise set normal mode bit */
  73. if (haptic->is_len_gpio_valid == true)
  74. gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 1);
  75. else {
  76. rc = isa1200_write_reg(haptic->client, ISA1200_HCTRL0,
  77. haptic->hctrl0_val | ~ISA1200_POWER_DOWN_MASK);
  78. if (rc < 0) {
  79. pr_err("%s: i2c write failure\n", __func__);
  80. return;
  81. }
  82. }
  83. if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
  84. int period_us = haptic->period_ns / 1000;
  85. rc = pwm_config(haptic->pwm,
  86. (period_us * haptic->pdata->duty) / 100,
  87. period_us);
  88. if (rc < 0) {
  89. pr_err("%s: pwm_config fail\n", __func__);
  90. goto chip_dwn;
  91. }
  92. rc = pwm_enable(haptic->pwm);
  93. if (rc < 0) {
  94. pr_err("%s: pwm_enable fail\n", __func__);
  95. goto chip_dwn;
  96. }
  97. } else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
  98. /* check for board specific clk callback */
  99. if (haptic->pdata->clk_enable) {
  100. rc = haptic->pdata->clk_enable(true);
  101. if (rc < 0) {
  102. pr_err("%s: clk enable cb failed\n",
  103. __func__);
  104. goto chip_dwn;
  105. }
  106. }
  107. mutex_lock(&haptic->lock_clk);
  108. /* vote for clock */
  109. if (haptic->pdata->need_pwm_clk && !haptic->clk_on) {
  110. rc = clk_prepare_enable(haptic->pwm_clk);
  111. if (rc < 0) {
  112. pr_err("%s: clk enable failed\n",
  113. __func__);
  114. mutex_unlock(&haptic->lock_clk);
  115. goto dis_clk_cb;
  116. }
  117. haptic->clk_on = true;
  118. }
  119. mutex_unlock(&haptic->lock_clk);
  120. rc = isa1200_write_reg(haptic->client,
  121. ISA1200_HCTRL5,
  122. ISA1200_HCTRL5_VIB_STRT);
  123. if (rc < 0) {
  124. pr_err("%s: start vibartion fail\n", __func__);
  125. goto dis_clk;
  126. }
  127. }
  128. } else {
  129. /* if hen and len are seperate then pull down hen
  130. * otherwise set power down bit */
  131. if (haptic->is_len_gpio_valid == true)
  132. gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
  133. else {
  134. rc = isa1200_write_reg(haptic->client, ISA1200_HCTRL0,
  135. haptic->hctrl0_val & ISA1200_POWER_DOWN_MASK);
  136. if (rc < 0) {
  137. pr_err("%s: i2c write failure\n", __func__);
  138. return;
  139. }
  140. }
  141. if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
  142. pwm_disable(haptic->pwm);
  143. } else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
  144. rc = isa1200_write_reg(haptic->client,
  145. ISA1200_HCTRL5,
  146. ISA1200_HCTRL5_VIB_STOP);
  147. if (rc < 0)
  148. pr_err("%s: stop vibartion fail\n", __func__);
  149. mutex_lock(&haptic->lock_clk);
  150. /* de-vote clock */
  151. if (haptic->pdata->need_pwm_clk && haptic->clk_on) {
  152. clk_disable_unprepare(haptic->pwm_clk);
  153. haptic->clk_on = false;
  154. }
  155. mutex_unlock(&haptic->lock_clk);
  156. /* check for board specific clk callback */
  157. if (haptic->pdata->clk_enable) {
  158. rc = haptic->pdata->clk_enable(false);
  159. if (rc < 0)
  160. pr_err("%s: clk disable cb failed\n",
  161. __func__);
  162. }
  163. }
  164. }
  165. return;
  166. dis_clk:
  167. mutex_lock(&haptic->lock_clk);
  168. if (haptic->pdata->need_pwm_clk && haptic->clk_on) {
  169. clk_disable_unprepare(haptic->pwm_clk);
  170. haptic->clk_on = false;
  171. }
  172. mutex_unlock(&haptic->lock_clk);
  173. dis_clk_cb:
  174. if (haptic->pdata->clk_enable) {
  175. rc = haptic->pdata->clk_enable(false);
  176. if (rc < 0)
  177. pr_err("%s: clk disable cb failed\n", __func__);
  178. }
  179. chip_dwn:
  180. if (haptic->is_len_gpio_valid == true)
  181. gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
  182. else {
  183. rc = isa1200_write_reg(haptic->client, ISA1200_HCTRL0,
  184. haptic->hctrl0_val & ISA1200_POWER_DOWN_MASK);
  185. if (rc < 0) {
  186. pr_err("%s: i2c write failure\n", __func__);
  187. return;
  188. }
  189. }
  190. }
  191. static void isa1200_chip_work(struct work_struct *work)
  192. {
  193. struct isa1200_chip *haptic;
  194. haptic = container_of(work, struct isa1200_chip, work);
  195. isa1200_vib_set(haptic, haptic->enable);
  196. }
  197. static void isa1200_chip_enable(struct timed_output_dev *dev, int value)
  198. {
  199. struct isa1200_chip *haptic = container_of(dev, struct isa1200_chip,
  200. dev);
  201. mutex_lock(&haptic->lock);
  202. hrtimer_cancel(&haptic->timer);
  203. if (value == 0)
  204. haptic->enable = 0;
  205. else {
  206. value = (value > haptic->pdata->max_timeout ?
  207. haptic->pdata->max_timeout : value);
  208. haptic->enable = 1;
  209. hrtimer_start(&haptic->timer,
  210. ktime_set(value / 1000, (value % 1000) * 1000000),
  211. HRTIMER_MODE_REL);
  212. }
  213. mutex_unlock(&haptic->lock);
  214. schedule_work(&haptic->work);
  215. }
  216. static int isa1200_chip_get_time(struct timed_output_dev *dev)
  217. {
  218. struct isa1200_chip *haptic = container_of(dev, struct isa1200_chip,
  219. dev);
  220. if (hrtimer_active(&haptic->timer)) {
  221. ktime_t r = hrtimer_get_remaining(&haptic->timer);
  222. struct timeval t = ktime_to_timeval(r);
  223. return t.tv_sec * 1000 + t.tv_usec / 1000;
  224. } else
  225. return 0;
  226. }
  227. static enum hrtimer_restart isa1200_vib_timer_func(struct hrtimer *timer)
  228. {
  229. struct isa1200_chip *haptic = container_of(timer, struct isa1200_chip,
  230. timer);
  231. haptic->enable = 0;
  232. schedule_work(&haptic->work);
  233. return HRTIMER_NORESTART;
  234. }
  235. static void dump_isa1200_reg(char *str, struct i2c_client *client)
  236. {
  237. pr_debug("%s reg0x%x=0x%x, reg0x%x=0x%x, reg0x%x=0x%x\n", str,
  238. ISA1200_HCTRL0, isa1200_read_reg(client, ISA1200_HCTRL0),
  239. ISA1200_HCTRL1, isa1200_read_reg(client, ISA1200_HCTRL1),
  240. ISA1200_HCTRL5, isa1200_read_reg(client, ISA1200_HCTRL5));
  241. }
  242. static int isa1200_setup(struct i2c_client *client)
  243. {
  244. struct isa1200_chip *haptic = i2c_get_clientdata(client);
  245. int temp, rc;
  246. u8 value;
  247. gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
  248. if (haptic->is_len_gpio_valid == true)
  249. gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
  250. udelay(250);
  251. gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 1);
  252. if (haptic->is_len_gpio_valid == true)
  253. gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 1);
  254. value = (haptic->pdata->smart_en << 3) |
  255. (haptic->pdata->is_erm << 5) |
  256. (haptic->pdata->ext_clk_en << 7);
  257. rc = isa1200_write_reg(client, ISA1200_HCTRL1, value);
  258. if (rc < 0) {
  259. pr_err("%s: i2c write failure\n", __func__);
  260. goto reset_gpios;
  261. }
  262. if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
  263. temp = haptic->pdata->pwm_fd.pwm_div;
  264. if (temp < 128 || temp > 1024 || temp % 128) {
  265. pr_err("%s: Invalid divider\n", __func__);
  266. goto reset_hctrl1;
  267. }
  268. value = ((temp >> 7) - 1);
  269. } else if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
  270. temp = haptic->pdata->pwm_fd.pwm_freq;
  271. if (temp < 22400 || temp > 172600 || temp % 22400) {
  272. pr_err("%s: Invalid frequency\n", __func__);
  273. goto reset_hctrl1;
  274. }
  275. value = ((temp / 22400) - 1);
  276. haptic->period_ns = NSEC_PER_SEC / temp;
  277. }
  278. value |= (haptic->pdata->mode_ctrl << 3) |
  279. (haptic->pdata->overdrive_high << 5) |
  280. (haptic->pdata->overdrive_en << 6) |
  281. (haptic->pdata->chip_en << 7);
  282. rc = isa1200_write_reg(client, ISA1200_HCTRL0, value);
  283. if (rc < 0) {
  284. pr_err("%s: i2c write failure\n", __func__);
  285. goto reset_hctrl1;
  286. }
  287. /* if hen and len are seperate then pull down hen
  288. * otherwise set power down bit */
  289. if (haptic->is_len_gpio_valid == true)
  290. gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
  291. else {
  292. rc = isa1200_write_reg(client, ISA1200_HCTRL0,
  293. value & ISA1200_POWER_DOWN_MASK);
  294. if (rc < 0) {
  295. pr_err("%s: i2c write failure\n", __func__);
  296. goto reset_hctrl1;
  297. }
  298. }
  299. haptic->hctrl0_val = value;
  300. dump_isa1200_reg("new:", client);
  301. return 0;
  302. reset_hctrl1:
  303. i2c_smbus_write_byte_data(client, ISA1200_HCTRL1,
  304. ISA1200_HCTRL1_RESET);
  305. reset_gpios:
  306. gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
  307. if (haptic->is_len_gpio_valid == true)
  308. gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
  309. return rc;
  310. }
  311. static int isa1200_reg_power(struct isa1200_chip *haptic, bool on)
  312. {
  313. const struct isa1200_regulator *reg_info =
  314. haptic->pdata->regulator_info;
  315. u8 i, num_reg = haptic->pdata->num_regulators;
  316. int rc;
  317. for (i = 0; i < num_reg; i++) {
  318. rc = regulator_set_optimum_mode(haptic->regs[i],
  319. on ? reg_info[i].load_uA : 0);
  320. if (rc < 0) {
  321. pr_err("%s: regulator_set_optimum_mode failed(%d)\n",
  322. __func__, rc);
  323. goto regs_fail;
  324. }
  325. rc = on ? regulator_enable(haptic->regs[i]) :
  326. regulator_disable(haptic->regs[i]);
  327. if (rc < 0) {
  328. pr_err("%s: regulator %sable fail %d\n", __func__,
  329. on ? "en" : "dis", rc);
  330. regulator_set_optimum_mode(haptic->regs[i],
  331. !on ? reg_info[i].load_uA : 0);
  332. goto regs_fail;
  333. }
  334. }
  335. return 0;
  336. regs_fail:
  337. while (i--) {
  338. regulator_set_optimum_mode(haptic->regs[i],
  339. !on ? reg_info[i].load_uA : 0);
  340. !on ? regulator_enable(haptic->regs[i]) :
  341. regulator_disable(haptic->regs[i]);
  342. }
  343. return rc;
  344. }
  345. static int isa1200_reg_setup(struct isa1200_chip *haptic, bool on)
  346. {
  347. const struct isa1200_regulator *reg_info =
  348. haptic->pdata->regulator_info;
  349. u8 i, num_reg = haptic->pdata->num_regulators;
  350. int rc = 0;
  351. /* put regulators */
  352. if (on == false) {
  353. i = num_reg;
  354. goto put_regs;
  355. }
  356. haptic->regs = kzalloc(num_reg * sizeof(struct regulator *),
  357. GFP_KERNEL);
  358. if (!haptic->regs) {
  359. pr_err("unable to allocate memory\n");
  360. return -ENOMEM;
  361. }
  362. for (i = 0; i < num_reg; i++) {
  363. haptic->regs[i] = regulator_get(&haptic->client->dev,
  364. reg_info[i].name);
  365. if (IS_ERR(haptic->regs[i])) {
  366. rc = PTR_ERR(haptic->regs[i]);
  367. pr_err("%s:regulator get failed(%d)\n", __func__, rc);
  368. goto put_regs;
  369. }
  370. if (regulator_count_voltages(haptic->regs[i]) > 0) {
  371. rc = regulator_set_voltage(haptic->regs[i],
  372. reg_info[i].min_uV, reg_info[i].max_uV);
  373. if (rc) {
  374. pr_err("%s: regulator_set_voltage failed(%d)\n",
  375. __func__, rc);
  376. regulator_put(haptic->regs[i]);
  377. goto put_regs;
  378. }
  379. }
  380. }
  381. return rc;
  382. put_regs:
  383. while (i--) {
  384. if (regulator_count_voltages(haptic->regs[i]) > 0)
  385. regulator_set_voltage(haptic->regs[i], 0,
  386. reg_info[i].max_uV);
  387. regulator_put(haptic->regs[i]);
  388. }
  389. kfree(haptic->regs);
  390. return rc;
  391. }
  392. #ifdef CONFIG_OF
  393. static int isa1200_parse_dt(struct device *dev,
  394. struct isa1200_platform_data *pdata)
  395. {
  396. struct device_node *temp, *np = dev->of_node;
  397. struct isa1200_regulator *reg_info;
  398. enum of_gpio_flags hap_en_flags = OF_GPIO_ACTIVE_LOW;
  399. enum of_gpio_flags hap_len_flags = OF_GPIO_ACTIVE_LOW;
  400. int rc = 0;
  401. u32 temp_val;
  402. const char *temp_string;
  403. rc = of_property_read_string(np, "label", &pdata->name);
  404. if (rc) {
  405. dev_err(dev, "Unable to read device name\n");
  406. return rc;
  407. }
  408. pdata->chip_en = of_property_read_bool(np, "imagis,chip-en");
  409. pdata->ext_clk_en = of_property_read_bool(np, "imagis,ext-clk-en");
  410. pdata->is_erm = of_property_read_bool(np, "imagis,is-erm");
  411. pdata->overdrive_high =
  412. of_property_read_bool(np, "imagis,overdrive-high");
  413. pdata->overdrive_en = of_property_read_bool(np, "imagis,overdrive-en");
  414. pdata->smart_en = of_property_read_bool(np, "imagis,smart-en");
  415. pdata->need_pwm_clk = of_property_read_bool(np, "imagis,need-pwm-clk");
  416. pdata->hap_en_gpio = of_get_named_gpio_flags(np,
  417. "imagis,hap-en-gpio", 0, &hap_en_flags);
  418. pdata->hap_len_gpio = of_get_named_gpio_flags(np,
  419. "imagis,hap-len-gpio", 0, &hap_len_flags);
  420. rc = of_property_read_u32(np, "imagis,max-timeout",
  421. &pdata->max_timeout);
  422. if (rc) {
  423. dev_err(dev, "Unable to read max timeout\n");
  424. return rc;
  425. }
  426. rc = of_property_read_u32(np, "imagis,pwm-div", &pdata->pwm_fd.pwm_div);
  427. if (rc && (rc != -EINVAL)) {
  428. dev_err(dev, "Unable to read pwm division\n");
  429. return rc;
  430. }
  431. rc = of_property_read_u32(np, "imagis,pwm-freq",
  432. &pdata->pwm_fd.pwm_freq);
  433. if (rc && (rc != -EINVAL)) {
  434. dev_err(dev, "Unable to read pwm frequency\n");
  435. return rc;
  436. }
  437. rc = of_property_read_u32(np, "imagis,pwm-ch-id", &pdata->pwm_ch_id);
  438. if (rc && (rc != -EINVAL)) {
  439. dev_err(dev, "Unable to read pwm channel id\n");
  440. return rc;
  441. }
  442. rc = of_property_read_u32(np, "imagis,mode-ctrl", &pdata->mode_ctrl);
  443. if (rc) {
  444. dev_err(dev, "Unable to read control mode\n");
  445. return rc;
  446. }
  447. rc = of_property_read_u32(np, "imagis,duty", &pdata->duty);
  448. if (rc && (rc != -EINVAL)) {
  449. dev_err(dev, "Unable to read duty cycle\n");
  450. return rc;
  451. }
  452. pdata->num_regulators = 0;
  453. temp = NULL;
  454. while ((temp = of_get_next_child(np, temp)))
  455. pdata->num_regulators++;
  456. if (!pdata->num_regulators)
  457. return 0;
  458. reg_info = devm_kzalloc(dev, pdata->num_regulators *
  459. sizeof(struct isa1200_regulator), GFP_KERNEL);
  460. if (!reg_info)
  461. return -ENOMEM;
  462. pdata->regulator_info = reg_info;
  463. for_each_child_of_node(np, temp) {
  464. rc = of_property_read_string(temp,
  465. "regulator-name", &temp_string);
  466. if (rc) {
  467. dev_err(dev, "Unable to read regulator name\n");
  468. return rc;
  469. } else
  470. reg_info->name = temp_string;
  471. rc = of_property_read_u32(temp, "regulator-max-microvolt",
  472. &temp_val);
  473. if (rc) {
  474. dev_err(dev, "Unable to read max uV\n");
  475. return rc;
  476. } else
  477. reg_info->max_uV = temp_val;
  478. rc = of_property_read_u32(temp, "regulator-min-microvolt",
  479. &temp_val);
  480. if (rc) {
  481. dev_err(dev, "Unable to read min uV\n");
  482. return rc;
  483. } else
  484. reg_info->min_uV = temp_val;
  485. rc = of_property_read_u32(temp, "regulator-max-microamp",
  486. &temp_val);
  487. if (rc) {
  488. dev_err(dev, "Unable to read load uA\n");
  489. return rc;
  490. } else
  491. reg_info->load_uA = temp_val;
  492. reg_info++;
  493. }
  494. return 0;
  495. }
  496. #else
  497. static int isa1200_parse_dt(struct device *dev,
  498. struct isa1200_platform_data *pdata)
  499. {
  500. return -ENODEV;
  501. }
  502. #endif
  503. static int __devinit isa1200_probe(struct i2c_client *client,
  504. const struct i2c_device_id *id)
  505. {
  506. struct isa1200_chip *haptic;
  507. struct isa1200_platform_data *pdata;
  508. int ret;
  509. if (!i2c_check_functionality(client->adapter,
  510. I2C_FUNC_SMBUS_BYTE_DATA)) {
  511. dev_err(&client->dev, "%s: no support for i2c read/write"
  512. "byte data\n", __func__);
  513. return -EIO;
  514. }
  515. if (client->dev.of_node) {
  516. pdata = devm_kzalloc(&client->dev,
  517. sizeof(struct isa1200_platform_data), GFP_KERNEL);
  518. if (!pdata) {
  519. dev_err(&client->dev, "Failed to allocate memory\n");
  520. return -ENOMEM;
  521. }
  522. ret = isa1200_parse_dt(&client->dev, pdata);
  523. if (ret) {
  524. dev_err(&client->dev, "Parsing DT failed(%d)", ret);
  525. return ret;
  526. }
  527. } else
  528. pdata = client->dev.platform_data;
  529. if (!pdata) {
  530. dev_err(&client->dev, "%s: no platform data\n", __func__);
  531. return -EINVAL;
  532. }
  533. if (pdata->dev_setup) {
  534. ret = pdata->dev_setup(true);
  535. if (ret < 0) {
  536. dev_err(&client->dev, "dev setup failed\n");
  537. return -EINVAL;
  538. }
  539. }
  540. haptic = kzalloc(sizeof(struct isa1200_chip), GFP_KERNEL);
  541. if (!haptic) {
  542. ret = -ENOMEM;
  543. goto mem_alloc_fail;
  544. }
  545. haptic->client = client;
  546. haptic->enable = 0;
  547. haptic->pdata = pdata;
  548. if (pdata->regulator_info) {
  549. ret = isa1200_reg_setup(haptic, true);
  550. if (ret) {
  551. dev_err(&client->dev, "%s: regulator setup failed\n",
  552. __func__);
  553. goto reg_setup_fail;
  554. }
  555. ret = isa1200_reg_power(haptic, true);
  556. if (ret) {
  557. dev_err(&client->dev, "%s: regulator power failed\n",
  558. __func__);
  559. goto reg_pwr_fail;
  560. }
  561. }
  562. if (pdata->power_on) {
  563. ret = pdata->power_on(1);
  564. if (ret) {
  565. dev_err(&client->dev, "%s: power-up failed\n",
  566. __func__);
  567. goto pwr_up_fail;
  568. }
  569. }
  570. mutex_init(&haptic->lock);
  571. mutex_init(&haptic->lock_clk);
  572. INIT_WORK(&haptic->work, isa1200_chip_work);
  573. haptic->clk_on = false;
  574. hrtimer_init(&haptic->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  575. haptic->timer.function = isa1200_vib_timer_func;
  576. /*register with timed output class*/
  577. haptic->dev.name = pdata->name;
  578. haptic->dev.get_time = isa1200_chip_get_time;
  579. haptic->dev.enable = isa1200_chip_enable;
  580. ret = timed_output_dev_register(&haptic->dev);
  581. if (ret < 0)
  582. goto timed_reg_fail;
  583. i2c_set_clientdata(client, haptic);
  584. ret = gpio_is_valid(pdata->hap_en_gpio);
  585. if (ret) {
  586. ret = gpio_request(pdata->hap_en_gpio, "haptic_en_gpio");
  587. if (ret) {
  588. dev_err(&client->dev, "%s: gpio %d request failed\n",
  589. __func__, pdata->hap_en_gpio);
  590. goto hen_gpio_fail;
  591. }
  592. } else {
  593. dev_err(&client->dev, "%s: Invalid gpio %d\n", __func__,
  594. pdata->hap_en_gpio);
  595. goto hen_gpio_fail;
  596. }
  597. haptic->is_len_gpio_valid = true;
  598. ret = gpio_is_valid(haptic->pdata->hap_len_gpio);
  599. if (ret) {
  600. ret = gpio_request(pdata->hap_len_gpio,
  601. "haptic_ldo_gpio");
  602. if (ret) {
  603. dev_err(&client->dev,
  604. "%s: gpio %d request failed\n",
  605. __func__, pdata->hap_len_gpio);
  606. goto len_gpio_fail;
  607. }
  608. } else {
  609. dev_err(&client->dev, "%s: gpio is not used/Invalid %d\n",
  610. __func__, pdata->hap_len_gpio);
  611. haptic->is_len_gpio_valid = false;
  612. }
  613. ret = isa1200_setup(client);
  614. if (ret) {
  615. dev_err(&client->dev, "%s: setup fail %d\n", __func__, ret);
  616. goto setup_fail;
  617. }
  618. if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
  619. haptic->pwm = pwm_request(pdata->pwm_ch_id, id->name);
  620. if (IS_ERR(haptic->pwm)) {
  621. dev_err(&client->dev, "%s: pwm request failed\n",
  622. __func__);
  623. ret = PTR_ERR(haptic->pwm);
  624. goto reset_hctrl0;
  625. }
  626. } else if (haptic->pdata->need_pwm_clk) {
  627. haptic->pwm_clk = clk_get(&client->dev, "pwm_clk");
  628. if (IS_ERR(haptic->pwm_clk)) {
  629. dev_err(&client->dev, "pwm_clk get failed\n");
  630. ret = PTR_ERR(haptic->pwm_clk);
  631. goto reset_hctrl0;
  632. }
  633. }
  634. printk(KERN_INFO "%s: %s registered\n", __func__, id->name);
  635. return 0;
  636. reset_hctrl0:
  637. gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
  638. if (haptic->is_len_gpio_valid == true)
  639. gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
  640. i2c_smbus_write_byte_data(client, ISA1200_HCTRL1,
  641. ISA1200_HCTRL1_RESET);
  642. i2c_smbus_write_byte_data(client, ISA1200_HCTRL0,
  643. ISA1200_HCTRL0_RESET);
  644. setup_fail:
  645. if (haptic->is_len_gpio_valid == true)
  646. gpio_free(pdata->hap_len_gpio);
  647. len_gpio_fail:
  648. gpio_free(pdata->hap_en_gpio);
  649. hen_gpio_fail:
  650. timed_output_dev_unregister(&haptic->dev);
  651. timed_reg_fail:
  652. mutex_destroy(&haptic->lock);
  653. if (pdata->power_on)
  654. pdata->power_on(0);
  655. pwr_up_fail:
  656. if (pdata->regulator_info)
  657. isa1200_reg_power(haptic, false);
  658. reg_pwr_fail:
  659. if (pdata->regulator_info)
  660. isa1200_reg_setup(haptic, false);
  661. reg_setup_fail:
  662. kfree(haptic);
  663. mem_alloc_fail:
  664. if (pdata->dev_setup)
  665. pdata->dev_setup(false);
  666. return ret;
  667. }
  668. static int __devexit isa1200_remove(struct i2c_client *client)
  669. {
  670. struct isa1200_chip *haptic = i2c_get_clientdata(client);
  671. hrtimer_cancel(&haptic->timer);
  672. cancel_work_sync(&haptic->work);
  673. /* turn-off current vibration */
  674. isa1200_vib_set(haptic, 0);
  675. if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE)
  676. pwm_free(haptic->pwm);
  677. timed_output_dev_unregister(&haptic->dev);
  678. gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
  679. if (haptic->is_len_gpio_valid == true)
  680. gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
  681. gpio_free(haptic->pdata->hap_en_gpio);
  682. if (haptic->is_len_gpio_valid == true)
  683. gpio_free(haptic->pdata->hap_len_gpio);
  684. /* reset hardware registers */
  685. i2c_smbus_write_byte_data(client, ISA1200_HCTRL0,
  686. ISA1200_HCTRL0_RESET);
  687. i2c_smbus_write_byte_data(client, ISA1200_HCTRL1,
  688. ISA1200_HCTRL1_RESET);
  689. /* destroy mutex */
  690. mutex_destroy(&haptic->lock);
  691. mutex_destroy(&haptic->lock_clk);
  692. /* power-off the chip */
  693. if (haptic->pdata->regulator_info) {
  694. isa1200_reg_power(haptic, false);
  695. isa1200_reg_setup(haptic, false);
  696. }
  697. if (haptic->pdata->power_on)
  698. haptic->pdata->power_on(0);
  699. if (haptic->pdata->dev_setup)
  700. haptic->pdata->dev_setup(false);
  701. kfree(haptic);
  702. return 0;
  703. }
  704. #ifdef CONFIG_PM
  705. static int isa1200_suspend(struct i2c_client *client, pm_message_t mesg)
  706. {
  707. struct isa1200_chip *haptic = i2c_get_clientdata(client);
  708. int ret;
  709. hrtimer_cancel(&haptic->timer);
  710. cancel_work_sync(&haptic->work);
  711. /* turn-off current vibration */
  712. isa1200_vib_set(haptic, 0);
  713. gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
  714. if (haptic->is_len_gpio_valid == true)
  715. gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
  716. if (haptic->pdata->regulator_info)
  717. isa1200_reg_power(haptic, false);
  718. if (haptic->pdata->power_on) {
  719. ret = haptic->pdata->power_on(0);
  720. if (ret) {
  721. dev_err(&client->dev, "power-down failed\n");
  722. return ret;
  723. }
  724. }
  725. return 0;
  726. }
  727. static int isa1200_resume(struct i2c_client *client)
  728. {
  729. struct isa1200_chip *haptic = i2c_get_clientdata(client);
  730. int ret;
  731. if (haptic->pdata->regulator_info)
  732. isa1200_reg_power(haptic, true);
  733. if (haptic->pdata->power_on) {
  734. ret = haptic->pdata->power_on(1);
  735. if (ret) {
  736. dev_err(&client->dev, "power-up failed\n");
  737. return ret;
  738. }
  739. }
  740. isa1200_setup(client);
  741. return 0;
  742. }
  743. #else
  744. #define isa1200_suspend NULL
  745. #define isa1200_resume NULL
  746. #endif
  747. static const struct i2c_device_id isa1200_id[] = {
  748. { "isa1200_1", 0 },
  749. { },
  750. };
  751. MODULE_DEVICE_TABLE(i2c, isa1200_id);
  752. #ifdef CONFIG_OF
  753. static struct of_device_id isa1200_match_table[] = {
  754. { .compatible = "imagis,isa1200",},
  755. { },
  756. };
  757. #else
  758. #define isa1200_match_table NULL
  759. #endif
  760. static struct i2c_driver isa1200_driver = {
  761. .driver = {
  762. .name = "isa1200",
  763. .of_match_table = isa1200_match_table,
  764. },
  765. .probe = isa1200_probe,
  766. .remove = __devexit_p(isa1200_remove),
  767. .suspend = isa1200_suspend,
  768. .resume = isa1200_resume,
  769. .id_table = isa1200_id,
  770. };
  771. static int __init isa1200_init(void)
  772. {
  773. return i2c_add_driver(&isa1200_driver);
  774. }
  775. static void __exit isa1200_exit(void)
  776. {
  777. i2c_del_driver(&isa1200_driver);
  778. }
  779. module_init(isa1200_init);
  780. module_exit(isa1200_exit);
  781. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  782. MODULE_DESCRIPTION("ISA1200 Haptic Motor driver");
  783. MODULE_LICENSE("GPL");