ss_vibrator.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /* Copyright (c) 2016, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/err.h>
  15. #include <linux/hrtimer.h>
  16. #include <linux/module.h>
  17. #include <linux/io.h>
  18. #include <linux/gpio.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/pinctrl/consumer.h>
  23. #include <linux/vibrator.h>
  24. #include <mach/msm_iomap.h>
  25. #include <linux/mfd/pm8xxx/pwm.h>
  26. #include <linux/mfd/pm8xxx/pm8921.h>
  27. #include "../staging/android/timed_output.h"
  28. #include "ss_vibrator.h"
  29. /* default timeout */
  30. #define VIB_DEFAULT_TIMEOUT 10000
  31. struct vibrator_platform_data vibrator_drvdata;
  32. struct ss_vib {
  33. struct device *dev;
  34. struct hrtimer vib_timer;
  35. struct timed_output_dev timed_dev;
  36. struct work_struct work;
  37. struct workqueue_struct *queue;
  38. struct mutex lock;
  39. int state;
  40. int timeout;
  41. int intensity;
  42. int timevalue;
  43. };
  44. void vibe_set_intensity(int intensity)
  45. {
  46. if (intensity == 0)
  47. vibe_pwm_onoff(0);
  48. else {
  49. if (intensity == MAX_INTENSITY)
  50. intensity = 1;
  51. else if (intensity != 0) {
  52. int tmp = MAX_INTENSITY - intensity;
  53. intensity = (tmp / 79); // 79 := 10000 / 127
  54. }
  55. if (vibrator_drvdata.is_pmic_vib_pwm){
  56. //PMIC PWM
  57. if (vib_config_pwm_device() < 0)
  58. pr_err("%s vib_config_pwm_device failed\n", __func__);
  59. } else {
  60. vibe_set_pwm_freq(intensity);
  61. vibe_pwm_onoff(1);
  62. }
  63. }
  64. }
  65. int32_t vibe_set_pwm_freq(int intensity)
  66. {
  67. int32_t calc_d;
  68. /* Put the MND counter in reset mode for programming */
  69. HWIO_OUTM(GP1_CFG_RCGR, HWIO_GP_SRC_SEL_VAL_BMSK,
  70. 0 << HWIO_GP_SRC_SEL_VAL_SHFT); //SRC_SEL = 000(cxo)
  71. #if defined(CONFIG_SEC_BERLUTI_PROJECT) || defined(CONFIG_MACH_S3VE3G_EUR)
  72. HWIO_OUTM(GP1_CFG_RCGR, HWIO_GP_SRC_DIV_VAL_BMSK,
  73. 23 << HWIO_GP_SRC_DIV_VAL_SHFT); //SRC_DIV = 10111 (Div 12)
  74. #else
  75. HWIO_OUTM(GP1_CFG_RCGR, HWIO_GP_SRC_DIV_VAL_BMSK,
  76. 31 << HWIO_GP_SRC_DIV_VAL_SHFT); //SRC_DIV = 11111 (Div 16)
  77. #endif
  78. HWIO_OUTM(GP1_CFG_RCGR, HWIO_GP_MODE_VAL_BMSK,
  79. 2 << HWIO_GP_MODE_VAL_SHFT); //Mode Select 10
  80. //M value
  81. HWIO_OUTM(GP_M_REG, HWIO_GP_MD_REG_M_VAL_BMSK,
  82. g_nlra_gp_clk_m << HWIO_GP_MD_REG_M_VAL_SHFT);
  83. #if defined(CONFIG_MACH_LT03EUR) || defined(CONFIG_MACH_LT03SKT)\
  84. || defined(CONFIG_MACH_LT03KTT) || defined(CONFIG_MACH_LT03LGT) || defined(CONFIG_MACH_PICASSO_LTE)
  85. if (intensity > 0){
  86. calc_d = g_nlra_gp_clk_n - (((intensity * g_nlra_gp_clk_pwm_mul) >> 8));
  87. if(calc_d < motor_min_strength)
  88. calc_d = motor_min_strength;
  89. else
  90. calc_d = (calc_d - motor_min_strength) \
  91. * (g_nlra_gp_clk_n * motor_strength / 100 - motor_min_strength) \
  92. / (g_nlra_gp_clk_n - motor_min_strength) + motor_min_strength;
  93. }
  94. else{
  95. calc_d = ((intensity * g_nlra_gp_clk_pwm_mul) >> 8) + g_nlra_gp_clk_d;
  96. if(g_nlra_gp_clk_n - calc_d > g_nlra_gp_clk_n * motor_strength /100)
  97. calc_d = g_nlra_gp_clk_n - g_nlra_gp_clk_n * motor_strength /100;
  98. }
  99. #else
  100. if (intensity > 0){
  101. calc_d = g_nlra_gp_clk_n - (((intensity * g_nlra_gp_clk_pwm_mul) >> 8));
  102. calc_d = calc_d * motor_strength /100;
  103. if(calc_d < motor_min_strength)
  104. calc_d = motor_min_strength;
  105. }
  106. else{
  107. calc_d = ((intensity * g_nlra_gp_clk_pwm_mul) >> 8) + g_nlra_gp_clk_d;
  108. if(g_nlra_gp_clk_n - calc_d > g_nlra_gp_clk_n * motor_strength /100)
  109. calc_d = g_nlra_gp_clk_n - g_nlra_gp_clk_n * motor_strength /100;
  110. }
  111. #endif
  112. // D value
  113. HWIO_OUTM(GP_D_REG, HWIO_GP_MD_REG_D_VAL_BMSK,
  114. (~((int16_t)calc_d << 1)) << HWIO_GP_MD_REG_D_VAL_SHFT);
  115. //N value
  116. HWIO_OUTM(GP_NS_REG, HWIO_GP_NS_REG_GP_N_VAL_BMSK,
  117. ~(g_nlra_gp_clk_n - g_nlra_gp_clk_m) << 0);
  118. return VIBRATION_SUCCESS;
  119. }
  120. int32_t vibe_pwm_onoff(u8 onoff)
  121. {
  122. if (onoff) {
  123. HWIO_OUTM(GP1_CMD_RCGR,HWIO_UPDATE_VAL_BMSK,
  124. 1 << HWIO_UPDATE_VAL_SHFT);//UPDATE ACTIVE
  125. HWIO_OUTM(GP1_CMD_RCGR,HWIO_ROOT_EN_VAL_BMSK,
  126. 1 << HWIO_ROOT_EN_VAL_SHFT);//ROOT_EN
  127. HWIO_OUTM(CAMSS_GP1_CBCR, HWIO_CLK_ENABLE_VAL_BMSK,
  128. 1 << HWIO_CLK_ENABLE_VAL_SHFT); //CLK_ENABLE
  129. } else {
  130. HWIO_OUTM(GP1_CMD_RCGR,HWIO_UPDATE_VAL_BMSK,
  131. 0 << HWIO_UPDATE_VAL_SHFT);
  132. HWIO_OUTM(GP1_CMD_RCGR,HWIO_ROOT_EN_VAL_BMSK,
  133. 0 << HWIO_ROOT_EN_VAL_SHFT);
  134. HWIO_OUTM(CAMSS_GP1_CBCR, HWIO_CLK_ENABLE_VAL_BMSK,
  135. 0 << HWIO_CLK_ENABLE_VAL_SHFT);
  136. }
  137. return VIBRATION_SUCCESS;
  138. }
  139. int vib_config_pwm_device(void)
  140. {
  141. int ret = 0;
  142. if(vibrator_drvdata.pwm_dev == NULL){
  143. //u32 pwm_period_us, duty_us;
  144. #if defined(CONFIG_MACH_HLTEDCM) || defined(CONFIG_MACH_HLTEKDI) || \
  145. defined(CONFIG_MACH_JS01LTEDCM) || defined(CONFIG_MACH_JS01LTESBM)
  146. vibrator_drvdata.pwm_dev = pwm_request(0,"lpg_3"); // 0 index for LPG3 channel.
  147. #else
  148. vibrator_drvdata.pwm_dev = pwm_request(0,"lpg_1"); // 0 index for LPG1 channel.
  149. #endif
  150. if (IS_ERR_OR_NULL(vibrator_drvdata.pwm_dev)) {
  151. pr_err("could not acquire PWM Channel 0, "
  152. "error %ld\n",PTR_ERR(vibrator_drvdata.pwm_dev));
  153. vibrator_drvdata.pwm_dev = NULL;
  154. return -ENODEV;
  155. }
  156. //pwm_period_us = 19; // 2000000;
  157. //duty_us = 18; //1000000; (90% Duty Cycle)
  158. ret = pwm_config(vibrator_drvdata.pwm_dev,
  159. vibrator_drvdata.duty_us,
  160. vibrator_drvdata.pwm_period_us);
  161. if (ret) {
  162. pr_err("pwm_config in vibrator enable failed %d\n", ret);
  163. return ret;
  164. }
  165. ret = pwm_enable(vibrator_drvdata.pwm_dev);
  166. if (ret < 0) {
  167. pr_err("pwm_enable in vibrator failed %d\n", ret);
  168. return ret;
  169. }
  170. } else {
  171. ret = pwm_enable(vibrator_drvdata.pwm_dev);
  172. if (ret < 0) {
  173. pr_err("pwm_enable in vibrator failed %d\n", ret);
  174. return ret;
  175. }
  176. }
  177. return ret;
  178. }
  179. static void set_vibrator(struct ss_vib *vib)
  180. {
  181. pr_info("[VIB]: %s, value[%d]\n", __func__, vib->state);
  182. if (vib->state) {
  183. if(vibrator_drvdata.is_pmic_vib_pwm){ //PMIC PWM
  184. gpio_set_value(vibrator_drvdata.vib_pwm_gpio, \
  185. VIBRATION_ON);
  186. } else { //AP PWM
  187. #if defined(CONFIG_MACH_HLTEDCM) || defined(CONFIG_MACH_HLTEKDI) || \
  188. defined(CONFIG_MACH_JS01LTEDCM) || defined(CONFIG_MACH_JS01LTESBM)
  189. gpio_tlmm_config(GPIO_CFG(vibrator_drvdata.vib_pwm_gpio,\
  190. 2, GPIO_CFG_OUTPUT, GPIO_CFG_PULL_DOWN, \
  191. GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  192. gpio_set_value(vibrator_drvdata.vib_pwm_gpio, \
  193. VIBRATION_ON);
  194. #elif defined(CONFIG_SEC_BERLUTI_PROJECT) || defined(CONFIG_MACH_S3VE3G_EUR) || defined(CONFIG_MACH_VICTOR3GDSDTV_LTN) || defined(CONFIG_SEC_HESTIA_PROJECT)
  195. gpio_tlmm_config(GPIO_CFG(vibrator_drvdata.vib_pwm_gpio,\
  196. 3, GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, \
  197. GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  198. gpio_set_value(vibrator_drvdata.vib_pwm_gpio, \
  199. VIBRATION_ON);
  200. #else
  201. gpio_tlmm_config(GPIO_CFG(vibrator_drvdata.vib_pwm_gpio,\
  202. 6, GPIO_CFG_OUTPUT, GPIO_CFG_PULL_DOWN, \
  203. GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  204. gpio_set_value(vibrator_drvdata.vib_pwm_gpio, \
  205. VIBRATION_ON);
  206. #endif
  207. }
  208. printk(KERN_DEBUG "[VIB] : %s\n", __func__);
  209. if (vibrator_drvdata.power_onoff) {
  210. if (!vibrator_drvdata.changed_chip)
  211. vibrator_drvdata.power_onoff(1);
  212. }
  213. #if defined(CONFIG_MOTOR_DRV_MAX77804K)
  214. if (vibrator_drvdata.changed_chip) {
  215. gpio_direction_output(vibrator_drvdata.changed_en_gpio, VIBRATION_ON);
  216. gpio_set_value(vibrator_drvdata.changed_en_gpio,VIBRATION_ON);
  217. }
  218. #elif defined(CONFIG_MOTOR_DRV_MAX77888)
  219. max77888_gpio_en(1);
  220. #elif defined(CONFIG_MOTOR_DRV_DRV2603)
  221. drv2603_gpio_en(1);
  222. #elif defined(CONFIG_MOTOR_ISA1000)
  223. gpio_direction_output(vibrator_drvdata.vib_en_gpio,VIBRATION_ON);
  224. gpio_set_value(vibrator_drvdata.vib_en_gpio,VIBRATION_ON);
  225. #endif
  226. hrtimer_start(&vib->vib_timer, ktime_set(vib->timevalue / 1000,
  227. (vib->timevalue % 1000) * 1000000),HRTIMER_MODE_REL);
  228. } else {
  229. if(vibrator_drvdata.is_pmic_vib_pwm){ //PMIC PWM
  230. gpio_set_value(vibrator_drvdata.vib_pwm_gpio, \
  231. VIBRATION_OFF);
  232. if(vibrator_drvdata.pwm_dev != NULL) //Disable the PWM device.
  233. pwm_disable(vibrator_drvdata.pwm_dev);
  234. } else{ //AP PWM
  235. #if defined(CONFIG_MACH_S3VE3G_EUR) || defined(CONFIG_MACH_VICTOR3GDSDTV_LTN)
  236. gpio_tlmm_config(GPIO_CFG(vibrator_drvdata.vib_pwm_gpio,\
  237. 0, GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, \
  238. GPIO_CFG_2MA),GPIO_CFG_ENABLE);
  239. gpio_set_value(vibrator_drvdata.vib_pwm_gpio, \
  240. VIBRATION_OFF);
  241. #else
  242. gpio_tlmm_config(GPIO_CFG(vibrator_drvdata.vib_pwm_gpio,\
  243. 0, GPIO_CFG_OUTPUT, GPIO_CFG_PULL_DOWN, \
  244. GPIO_CFG_2MA),GPIO_CFG_ENABLE);
  245. gpio_set_value(vibrator_drvdata.vib_pwm_gpio, \
  246. VIBRATION_OFF);
  247. #endif
  248. }
  249. printk(KERN_DEBUG "[VIB] : %s\n", __func__);
  250. if (vibrator_drvdata.power_onoff) {
  251. if (!vibrator_drvdata.changed_chip)
  252. vibrator_drvdata.power_onoff(0);
  253. }
  254. #if defined(CONFIG_MOTOR_DRV_MAX77804K)
  255. if (vibrator_drvdata.changed_chip) {
  256. gpio_direction_output(vibrator_drvdata.changed_en_gpio, VIBRATION_OFF);
  257. gpio_set_value(vibrator_drvdata.changed_en_gpio,VIBRATION_OFF);
  258. }
  259. #elif defined(CONFIG_MOTOR_DRV_MAX77888)
  260. max77888_gpio_en(0);
  261. #elif defined(CONFIG_MOTOR_DRV_DRV2603)
  262. drv2603_gpio_en(0);
  263. #elif defined(CONFIG_MOTOR_ISA1000)
  264. gpio_direction_output(vibrator_drvdata.vib_en_gpio,VIBRATION_OFF);
  265. gpio_set_value(vibrator_drvdata.vib_en_gpio,VIBRATION_OFF);
  266. #endif
  267. }
  268. }
  269. static void vibrator_enable(struct timed_output_dev *dev, int value)
  270. {
  271. struct ss_vib *vib = container_of(dev, struct ss_vib, timed_dev);
  272. mutex_lock(&vib->lock);
  273. hrtimer_cancel(&vib->vib_timer);
  274. if (value == 0) {
  275. pr_info("[VIB]: OFF\n");
  276. vib->state = 0;
  277. } else {
  278. pr_info("[VIB]: ON, Duration : %d msec, intensity : %d\n",
  279. value, vib->intensity);
  280. vib->state = 1;
  281. vib->timevalue = value;
  282. }
  283. mutex_unlock(&vib->lock);
  284. queue_work(vib->queue, &vib->work);
  285. }
  286. static void ss_vibrator_update(struct work_struct *work)
  287. {
  288. struct ss_vib *vib = container_of(work, struct ss_vib, work);
  289. set_vibrator(vib);
  290. }
  291. static int vibrator_get_time(struct timed_output_dev *dev)
  292. {
  293. struct ss_vib *vib = container_of(dev, struct ss_vib, timed_dev);
  294. if (hrtimer_active(&vib->vib_timer)) {
  295. ktime_t r = hrtimer_get_remaining(&vib->vib_timer);
  296. return (int)ktime_to_us(r);
  297. } else
  298. return 0;
  299. }
  300. static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
  301. {
  302. struct ss_vib *vib = container_of(timer, struct ss_vib, vib_timer);
  303. vib->state = 0;
  304. queue_work(vib->queue, &vib->work);
  305. return HRTIMER_NORESTART;
  306. }
  307. #ifdef CONFIG_PM
  308. static int ss_vibrator_suspend(struct device *dev)
  309. {
  310. struct ss_vib *vib = dev_get_drvdata(dev);
  311. pr_info("[VIB]: %s\n", __func__);
  312. hrtimer_cancel(&vib->vib_timer);
  313. cancel_work_sync(&vib->work);
  314. /* turn-off vibrator */
  315. vib->state = 0;
  316. set_vibrator(vib);
  317. #if defined(CONFIG_MOTOR_DRV_MAX77803)
  318. max77803_vibtonz_en(0);
  319. #elif defined(CONFIG_MOTOR_DRV_MAX77804K)
  320. if (!vibrator_drvdata.changed_chip)
  321. max77804k_vibtonz_en(0);
  322. #elif defined(CONFIG_MOTOR_DRV_MAX77828)
  323. max77828_vibtonz_en(0);
  324. #elif defined(CONFIG_MOTOR_DRV_MAX77888)
  325. max77888_vibtonz_en(0);
  326. #endif
  327. return 0;
  328. }
  329. static int ss_vibrator_resume(struct device *dev)
  330. {
  331. struct ss_vib *vib = dev_get_drvdata(dev);
  332. pr_info("[VIB]: %s, intensity : %d\n", __func__, vib->intensity);
  333. #if defined(CONFIG_MOTOR_DRV_MAX77803)
  334. max77803_vibtonz_en(1);
  335. #elif defined(CONFIG_MOTOR_DRV_MAX77804K)
  336. if (!vibrator_drvdata.changed_chip)
  337. max77804k_vibtonz_en(1);
  338. #elif defined(CONFIG_MOTOR_DRV_MAX77828)
  339. max77828_vibtonz_en(1);
  340. #elif defined(CONFIG_MOTOR_DRV_MAX77888)
  341. max77888_vibtonz_en(1);
  342. #endif
  343. return 0;
  344. }
  345. #endif
  346. static SIMPLE_DEV_PM_OPS(vibrator_pm_ops, ss_vibrator_suspend, ss_vibrator_resume);
  347. static int vibrator_parse_dt(struct ss_vib *vib)
  348. {
  349. struct device_node *np = vib->dev->of_node;
  350. int rc;
  351. #if defined(CONFIG_MACH_HLTEDCM) || defined(CONFIG_MACH_HLTEKDI) || defined (CONFIG_MACH_JS01LTEDCM)
  352. vibrator_drvdata.vib_pwm_gpio = of_get_named_gpio(np, "samsung,pmic_vib_pwm_jpn", 0);
  353. #else
  354. vibrator_drvdata.vib_pwm_gpio = of_get_named_gpio(np, "samsung,pmic_vib_pwm", 0);
  355. #endif
  356. if (!gpio_is_valid(vibrator_drvdata.vib_pwm_gpio)) {
  357. pr_err("%s:%d, reset gpio not specified\n",
  358. __func__, __LINE__);
  359. }
  360. #if defined(CONFIG_MOTOR_ISA1000)
  361. vibrator_drvdata.vib_en_gpio = of_get_named_gpio(np, "samsung,vib_en_gpio", 0);
  362. #endif
  363. #if defined(CONFIG_MOTOR_DRV_DRV2603)
  364. vibrator_drvdata.drv2603_en_gpio = of_get_named_gpio(np, "samsung,drv2603_en", 0);
  365. if (!gpio_is_valid(vibrator_drvdata.drv2603_en_gpio)) {
  366. pr_err("%s:%d, drv2603_en_gpio not specified\n",
  367. __func__, __LINE__);
  368. }
  369. #endif
  370. #if defined(CONFIG_MOTOR_DRV_MAX77888)
  371. vibrator_drvdata.max77888_en_gpio = of_get_named_gpio(np, "samsung,vib_power_en", 0);
  372. if (!gpio_is_valid(vibrator_drvdata.max77888_en_gpio)) {
  373. pr_err("%s:%d, max77888_en_gpio not specified\n",__func__, __LINE__);
  374. }
  375. #endif
  376. rc = of_property_read_u32(np, "samsung,pmic_vib_en", &vibrator_drvdata.is_pmic_vib_en);
  377. if (rc) {
  378. pr_err("%s:%d, is_pmic_vib_en not specified\n",
  379. __func__, __LINE__);
  380. return -EINVAL;
  381. }
  382. rc = of_property_read_u32(np, "samsung,pmic_haptic_pwr_en", &vibrator_drvdata.is_pmic_haptic_pwr_en);
  383. if (rc) {
  384. pr_err("%s:%d, is_pmic_haptic_pwr_en not specified\n",
  385. __func__, __LINE__);
  386. return -EINVAL;
  387. }
  388. //vibrator_drvdata.is_pmic_vib_pwm = 0; AP PWM PIN
  389. //vibrator_drvdata.is_pmic_vib_pwm = 1; PMIC PWM PIN
  390. rc = of_property_read_u32(np, "samsung,is_pmic_vib_pwm", &vibrator_drvdata.is_pmic_vib_pwm);
  391. if (rc) {
  392. pr_err("%s:%d, is_pmic_vib_pwm not specified\n",
  393. __func__, __LINE__);
  394. return -EINVAL;
  395. }
  396. rc = of_property_read_u32(np, "samsung,pwm_period_us", &vibrator_drvdata.pwm_period_us);
  397. if (rc) {
  398. pr_err("%s:%d, pwm_period_us not specified\n",
  399. __func__, __LINE__);
  400. return -EINVAL;
  401. }
  402. rc = of_property_read_u32(np, "samsung,duty_us", &vibrator_drvdata.duty_us);
  403. if (rc) {
  404. pr_err("%s:%d, duty_us not specified\n",
  405. __func__, __LINE__);
  406. return -EINVAL;
  407. }
  408. rc = of_property_read_u32(np, "samsung,changed_chip", &vibrator_drvdata.changed_chip);
  409. if (rc) {
  410. pr_info("%s:%d, changed_chip not specified\n", __func__, __LINE__);
  411. vibrator_drvdata.changed_chip = 0;
  412. rc = 0;
  413. } else {
  414. if (vibrator_drvdata.changed_chip)
  415. vibrator_drvdata.changed_en_gpio = of_get_named_gpio(np, "samsung,changed_en_gpio", 0);
  416. }
  417. return rc;
  418. }
  419. #if defined(CONFIG_MOTOR_DRV_MAX77804K) || defined(CONFIG_MOTOR_DRV_MAX77828)
  420. static void max77803_haptic_power_onoff(int onoff)
  421. {
  422. int ret;
  423. static struct regulator *reg_l23;
  424. if (!reg_l23) {
  425. reg_l23 = regulator_get(NULL, "8084_l23");
  426. ret = regulator_set_voltage(reg_l23, 3000000, 3000000);
  427. if (IS_ERR(reg_l23)) {
  428. printk(KERN_ERR"could not get 8084_l23, rc = %ld\n",
  429. PTR_ERR(reg_l23));
  430. return;
  431. }
  432. }
  433. if (onoff) {
  434. ret = regulator_enable(reg_l23);
  435. if (ret) {
  436. printk(KERN_ERR"enable l23 failed, rc=%d\n", ret);
  437. return;
  438. }
  439. printk(KERN_DEBUG"haptic power_on is finished.\n");
  440. } else {
  441. if (regulator_is_enabled(reg_l23)) {
  442. ret = regulator_disable(reg_l23);
  443. if (ret) {
  444. printk(KERN_ERR"disable l23 failed, rc=%d\n",
  445. ret);
  446. return;
  447. }
  448. }
  449. printk(KERN_DEBUG"haptic power_off is finished.\n");
  450. }
  451. }
  452. #endif
  453. #if defined(CONFIG_MOTOR_DRV_MAX77803)
  454. static void max77803_haptic_power_onoff(int onoff)
  455. {
  456. int ret;
  457. #if defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_MONTBLANC_PROJECT) || defined(CONFIG_SEC_JS_PROJECT) || \
  458. defined(CONFIG_MACH_FLTEEUR) || defined(CONFIG_MACH_FLTESKT) || defined(CONFIG_MACH_JVELTEEUR) ||\
  459. defined(CONFIG_MACH_VIKALCU) || defined(CONFIG_SEC_LOCALE_KOR_FRESCO)
  460. static struct regulator *reg_l23;
  461. if (!reg_l23) {
  462. reg_l23 = regulator_get(NULL, "8941_l23");
  463. #if defined(CONFIG_MACH_FLTESKT)
  464. ret = regulator_set_voltage(reg_l23, 3000000, 3000000);
  465. #elif defined(CONFIG_MACH_HLTEVZW)
  466. ret = regulator_set_voltage(reg_l23, 3100000, 3100000);
  467. #elif defined(CONFIG_SEC_LOCALE_KOR_FRESCO)
  468. ret = regulator_set_voltage(reg_l23, 2488000,2488000);
  469. #else
  470. ret = regulator_set_voltage(reg_l23, 2825000, 2825000);
  471. #endif
  472. if (IS_ERR(reg_l23)) {
  473. printk(KERN_ERR"could not get 8941_l23, rc = %ld\n",
  474. PTR_ERR(reg_l23));
  475. return;
  476. }
  477. }
  478. if (onoff) {
  479. ret = regulator_enable(reg_l23);
  480. if (ret) {
  481. printk(KERN_ERR"enable l23 failed, rc=%d\n", ret);
  482. return;
  483. }
  484. printk(KERN_DEBUG"haptic power_on is finished.\n");
  485. } else {
  486. if (regulator_is_enabled(reg_l23)) {
  487. ret = regulator_disable(reg_l23);
  488. if (ret) {
  489. printk(KERN_ERR"disable l23 failed, rc=%d\n",
  490. ret);
  491. return;
  492. }
  493. }
  494. printk(KERN_DEBUG"haptic power_off is finished.\n");
  495. }
  496. #else
  497. static struct regulator *reg_l17;
  498. if (!reg_l17) {
  499. reg_l17 = regulator_get(NULL, "8941_l17");
  500. ret = regulator_set_voltage(reg_l17, 3000000, 3000000);
  501. if (IS_ERR(reg_l17)) {
  502. printk(KERN_ERR"could not get 8941_l17, rc = %ld\n",
  503. PTR_ERR(reg_l17));
  504. return;
  505. }
  506. }
  507. if (onoff) {
  508. ret = regulator_enable(reg_l17);
  509. if (ret) {
  510. printk(KERN_ERR"enable l17 failed, rc=%d\n", ret);
  511. return;
  512. }
  513. printk(KERN_DEBUG"haptic power_on is finished.\n");
  514. } else {
  515. if (regulator_is_enabled(reg_l17)) {
  516. ret = regulator_disable(reg_l17);
  517. if (ret) {
  518. printk(KERN_ERR"disable l17 failed, rc=%d\n",
  519. ret);
  520. return;
  521. }
  522. }
  523. printk(KERN_DEBUG"haptic power_off is finished.\n");
  524. }
  525. #endif
  526. }
  527. #endif
  528. #if defined(CONFIG_MOTOR_DRV_DRV2603)
  529. void drv2603_gpio_en(bool en)
  530. {
  531. if (en) {
  532. gpio_direction_output(vibrator_drvdata.drv2603_en_gpio, 1);
  533. } else {
  534. gpio_direction_output(vibrator_drvdata.drv2603_en_gpio, 0);
  535. }
  536. }
  537. static int32_t drv2603_gpio_init(void)
  538. {
  539. int ret;
  540. ret = gpio_request(vibrator_drvdata.drv2603_en_gpio, "vib enable");
  541. if (ret < 0) {
  542. printk(KERN_ERR "vib enable gpio_request is failed\n");
  543. return 1;
  544. }
  545. return 0;
  546. }
  547. #endif
  548. #if defined(CONFIG_MOTOR_DRV_MAX77888)
  549. void max77888_gpio_en(bool en)
  550. {
  551. if (en) {
  552. gpio_direction_output(vibrator_drvdata.max77888_en_gpio, 1);
  553. } else {
  554. gpio_direction_output(vibrator_drvdata.max77888_en_gpio, 0);
  555. }
  556. }
  557. static int32_t max77888_gpio_init(void)
  558. {
  559. int ret;
  560. ret = gpio_request(vibrator_drvdata.max77888_en_gpio, "vib enable");
  561. if (ret < 0) {
  562. printk(KERN_ERR "vib enable gpio_request is failed\n");
  563. return 1;
  564. }
  565. return 0;
  566. }
  567. #endif
  568. static void vibrator_initialize(void)
  569. {
  570. int ret;
  571. /* set gpio config */
  572. if (vibrator_drvdata.is_pmic_vib_pwm) { //PMIC PWM
  573. ret = gpio_request(vibrator_drvdata.vib_pwm_gpio, \
  574. "vib pwm");
  575. if (ret < 0) {
  576. printk(KERN_ERR"vib pwm gpio_request is failed\n");
  577. goto err2;
  578. }
  579. ret = pm8xxx_gpio_config(vibrator_drvdata.vib_pwm_gpio,\
  580. &vib_pwm);
  581. if (ret < 0) {
  582. printk(KERN_ERR "failed to configure vib pwm pmic gpio\n");
  583. goto err2;
  584. }
  585. } else { //AP PWM
  586. #if defined(CONFIG_MACH_S3VE3G_EUR)
  587. gpio_tlmm_config(GPIO_CFG(vibrator_drvdata.vib_pwm_gpio,
  588. 0, GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_2MA),
  589. GPIO_CFG_ENABLE);
  590. #else
  591. gpio_tlmm_config(GPIO_CFG(vibrator_drvdata.vib_pwm_gpio,
  592. 0, GPIO_CFG_OUTPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA),
  593. GPIO_CFG_ENABLE);
  594. #endif
  595. }
  596. #if defined(CONFIG_MOTOR_ISA1000)
  597. if (!vibrator_drvdata.is_pmic_vib_en) {
  598. ret = gpio_request(vibrator_drvdata.vib_en_gpio,"vib enable");
  599. if (ret < 0) {
  600. printk(KERN_ERR "vib enable gpio_request is failed\n");
  601. goto err2;
  602. }
  603. }
  604. #endif
  605. #if defined(CONFIG_MOTOR_DRV_DRV2603)
  606. if (drv2603_gpio_init())
  607. goto err2;
  608. #elif defined(CONFIG_MOTOR_DRV_MAX77888)
  609. if(max77888_gpio_init())
  610. goto err2;
  611. #endif
  612. return;
  613. err2:
  614. printk(KERN_ERR "%s failed check.\n", __func__);
  615. }
  616. static struct device *vib_dev;
  617. extern struct class *sec_class;
  618. static ssize_t show_vib_tuning(struct device *dev,
  619. struct device_attribute *attr, char *buf)
  620. {
  621. sprintf(buf, "gp_m %d, gp_n %d, gp_d %d, pwm_mul %d, strength %d, min_str %d\n",
  622. g_nlra_gp_clk_m, g_nlra_gp_clk_n, g_nlra_gp_clk_d,
  623. g_nlra_gp_clk_pwm_mul, motor_strength, motor_min_strength);
  624. return strlen(buf);
  625. }
  626. static ssize_t store_vib_tuning(struct device *dev,
  627. struct device_attribute *attr, const char *buf, size_t count)
  628. {
  629. int retval;
  630. int temp_m, temp_n, temp_str;
  631. retval = sscanf(buf, "%d %d %d", &temp_m, &temp_n, &temp_str);
  632. if (retval == 0) {
  633. pr_info("[VIB]: %s, fail to get vib_tuning value\n", __func__);
  634. return count;
  635. }
  636. g_nlra_gp_clk_m = temp_m;
  637. g_nlra_gp_clk_n = temp_n;
  638. g_nlra_gp_clk_d = temp_n / 2;
  639. g_nlra_gp_clk_pwm_mul = temp_n;
  640. motor_strength = temp_str;
  641. motor_min_strength = g_nlra_gp_clk_n*MOTOR_MIN_STRENGTH/100;
  642. pr_info("[VIB]: %s gp_m %d, gp_n %d, gp_d %d, pwm_mul %d, strength %d, min_str %d\n", __func__,
  643. g_nlra_gp_clk_m, g_nlra_gp_clk_n, g_nlra_gp_clk_d,
  644. g_nlra_gp_clk_pwm_mul, motor_strength, motor_min_strength);
  645. return count;
  646. }
  647. static DEVICE_ATTR(vib_tuning, 0660, show_vib_tuning, store_vib_tuning);
  648. static ssize_t intensity_store(struct device *dev,
  649. struct device_attribute *devattr, const char *buf, size_t count)
  650. {
  651. struct timed_output_dev *t_dev = dev_get_drvdata(dev);
  652. struct ss_vib *vib = container_of(t_dev, struct ss_vib, timed_dev);
  653. int ret = 0, set_intensity = 0;
  654. ret = kstrtoint(buf, 0, &set_intensity);
  655. if ((set_intensity < 0) || (set_intensity > (MAX_INTENSITY / 100))) {
  656. pr_err("[VIB]: %sout of range\n", __func__);
  657. return -EINVAL;
  658. }
  659. vibe_set_intensity((set_intensity * 100));
  660. vib->intensity = (set_intensity * 100);
  661. return count;
  662. }
  663. static ssize_t intensity_show(struct device *dev,
  664. struct device_attribute *attr, char *buf)
  665. {
  666. struct timed_output_dev *t_dev = dev_get_drvdata(dev);
  667. struct ss_vib *vib = container_of(t_dev, struct ss_vib, timed_dev);
  668. return sprintf(buf, "%u\n", (vib->intensity / 100));
  669. }
  670. static ssize_t pwm_default_show(struct device *dev,
  671. struct device_attribute *attr, char *buf)
  672. {
  673. return sprintf(buf, "%u\n", (DEFAULT_INTENSITY / 100));
  674. }
  675. static ssize_t pwm_max_show(struct device *dev,
  676. struct device_attribute *attr, char *buf)
  677. {
  678. return sprintf(buf, "%u\n", (MAX_INTENSITY / 100));
  679. }
  680. static ssize_t pwm_min_show(struct device *dev,
  681. struct device_attribute *attr, char *buf)
  682. {
  683. return sprintf(buf, "%u\n", 0);
  684. }
  685. static ssize_t pwm_threshold_show(struct device *dev,
  686. struct device_attribute *attr, char *buf)
  687. {
  688. return sprintf(buf, "%u\n", 75);
  689. }
  690. static DEVICE_ATTR(pwm_default, 0444, pwm_default_show, NULL);
  691. static DEVICE_ATTR(pwm_max, 0444, pwm_max_show, NULL);
  692. static DEVICE_ATTR(pwm_min, 0444, pwm_min_show, NULL);
  693. static DEVICE_ATTR(pwm_threshold, 0444, pwm_threshold_show, NULL);
  694. static DEVICE_ATTR(pwm_value, 0644, intensity_show, intensity_store);
  695. static int ss_vibrator_probe(struct platform_device *pdev)
  696. {
  697. struct ss_vib *vib;
  698. int rc = 0;
  699. pr_info("[VIB]: %s\n",__func__);
  700. motor_min_strength = g_nlra_gp_clk_n*MOTOR_MIN_STRENGTH/100;
  701. vib = devm_kzalloc(&pdev->dev, sizeof(*vib), GFP_KERNEL);
  702. if (!vib) {
  703. pr_err("[VIB]: %s : Failed to allocate memory\n", __func__);
  704. return -ENOMEM;
  705. }
  706. if (!pdev->dev.of_node) {
  707. pr_err("[VIB]: %s failed, DT is NULL", __func__);
  708. return -ENODEV;
  709. }
  710. vib->dev = &pdev->dev;
  711. rc = vibrator_parse_dt(vib);
  712. if(rc)
  713. return rc;
  714. #if defined(CONFIG_MACH_HLTEDCM) || defined(CONFIG_MACH_HLTEKDI) || defined(CONFIG_MACH_JS01LTEDCM)
  715. virt_mmss_gp1_base = ioremap(MSM_MMSS_GP3_BASE,0x28);
  716. #elif defined(CONFIG_SEC_BERLUTI_PROJECT) || defined(CONFIG_MACH_S3VE3G_EUR)
  717. virt_mmss_gp1_base = ioremap(MSM_MMSS_GP0_BASE,0x28);
  718. #else
  719. virt_mmss_gp1_base = ioremap(MSM_MMSS_GP1_BASE,0x28);
  720. #endif
  721. if (!virt_mmss_gp1_base)
  722. panic("[VIB]: Unable to ioremap MSM_MMSS_GP1 memory!");
  723. #if defined(CONFIG_MOTOR_DRV_MAX77803) || defined(CONFIG_MOTOR_DRV_MAX77804K) || defined(CONFIG_MOTOR_DRV_MAX77828)
  724. vibrator_drvdata.power_onoff = max77803_haptic_power_onoff;
  725. #else
  726. vibrator_drvdata.power_onoff = NULL;
  727. #endif
  728. vibrator_drvdata.pwm_dev = NULL;
  729. vib->state = 0;
  730. vib->intensity = DEFAULT_INTENSITY;
  731. vib->timeout = VIB_DEFAULT_TIMEOUT;
  732. vibrator_initialize();
  733. vibe_set_intensity(vib->intensity);
  734. INIT_WORK(&vib->work, ss_vibrator_update);
  735. mutex_init(&vib->lock);
  736. vib->queue = create_singlethread_workqueue("ss_vibrator");
  737. if (!vib->queue) {
  738. pr_err("[VIB]: %s: can't create workqueue\n", __func__);
  739. return -ENOMEM;
  740. }
  741. hrtimer_init(&vib->vib_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  742. vib->vib_timer.function = vibrator_timer_func;
  743. vib->timed_dev.name = "vibrator";
  744. vib->timed_dev.get_time = vibrator_get_time;
  745. vib->timed_dev.enable = vibrator_enable;
  746. dev_set_drvdata(&pdev->dev, vib);
  747. rc = timed_output_dev_register(&vib->timed_dev);
  748. if (rc < 0) {
  749. pr_err("[VIB]: timed_output_dev_register fail (rc=%d)\n", rc);
  750. goto err_read_vib;
  751. }
  752. rc = sysfs_create_file(&vib->timed_dev.dev->kobj, &dev_attr_pwm_default.attr);
  753. rc = sysfs_create_file(&vib->timed_dev.dev->kobj, &dev_attr_pwm_min.attr);
  754. rc = sysfs_create_file(&vib->timed_dev.dev->kobj, &dev_attr_pwm_max.attr);
  755. rc = sysfs_create_file(&vib->timed_dev.dev->kobj, &dev_attr_pwm_threshold.attr);
  756. rc = sysfs_create_file(&vib->timed_dev.dev->kobj, &dev_attr_pwm_value.attr);
  757. if (rc < 0) {
  758. pr_err("[VIB]: Failed to register sysfs intensity: %d\n", rc);
  759. }
  760. vib_dev = device_create(sec_class, NULL, 0, NULL, "vib");
  761. if (IS_ERR(vib_dev)) {
  762. pr_info("[VIB]: Failed to create device for samsung vib\n");
  763. }
  764. rc = sysfs_create_file(&vib_dev->kobj, &dev_attr_vib_tuning.attr);
  765. if (rc) {
  766. pr_info("Failed to create sysfs group for samsung specific led\n");
  767. }
  768. return 0;
  769. err_read_vib:
  770. iounmap(virt_mmss_gp1_base);
  771. destroy_workqueue(vib->queue);
  772. mutex_destroy(&vib->lock);
  773. return rc;
  774. }
  775. static int ss_vibrator_remove(struct platform_device *pdev)
  776. {
  777. struct ss_vib *vib = dev_get_drvdata(&pdev->dev);
  778. iounmap(virt_mmss_gp1_base);
  779. destroy_workqueue(vib->queue);
  780. mutex_destroy(&vib->lock);
  781. return 0;
  782. }
  783. static const struct of_device_id vib_motor_match[] = {
  784. { .compatible = "vibrator",
  785. },
  786. {}
  787. };
  788. static struct platform_driver ss_vibrator_platdrv =
  789. {
  790. .driver =
  791. {
  792. .name = "vibrator",
  793. .owner = THIS_MODULE,
  794. .of_match_table = vib_motor_match,
  795. .pm = &vibrator_pm_ops,
  796. },
  797. .probe = ss_vibrator_probe,
  798. .remove = ss_vibrator_remove,
  799. };
  800. static int __init ss_timed_vibrator_init(void)
  801. {
  802. return platform_driver_register(&ss_vibrator_platdrv);
  803. }
  804. void __exit ss_timed_vibrator_exit(void)
  805. {
  806. platform_driver_unregister(&ss_vibrator_platdrv);
  807. }
  808. module_init(ss_timed_vibrator_init);
  809. module_exit(ss_timed_vibrator_exit);
  810. MODULE_AUTHOR("Samsung Corporation");
  811. MODULE_DESCRIPTION("timed output vibrator device");
  812. MODULE_LICENSE("GPL v2");