isa1200_vibrator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* drivers/motor/isa1200_vibrator.c
  2. *
  3. * Copyright (C) 2011 Samsung Electronics Co. Ltd. All Rights Reserved.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/timer.h>
  18. #include <linux/fs.h>
  19. #include <linux/version.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/hrtimer.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include <linux/clk.h>
  27. #include <linux/wakelock.h>
  28. #include <linux/io.h>
  29. #include <linux/vibrator.h>
  30. #include <linux/i2c.h>
  31. #include <linux/earlysuspend.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/clk.h>
  34. #include <linux/delay.h>
  35. #include <linux/regulator/machine.h>
  36. #include <linux/regulator/krait-regulator.h>
  37. #include "../staging/android/timed_output.h"
  38. #include "isa1200_vibrator.h"
  39. static struct hrtimer timer;
  40. static int max_timeout = 10000;
  41. static int vibrator_value = 0;
  42. static int vibrator_work;
  43. static int isa1200_enabled;
  44. static void _set_vibrator_work(struct work_struct *unused);
  45. static DECLARE_WORK(vib_work, _set_vibrator_work);
  46. struct vibrator_platform_data_isa1200 vibrator_drvdata;
  47. #if defined (CONFIG_MACH_MATISSE3G_OPEN) || defined (CONFIG_SEC_MATISSELTE_COMMON) || defined (CONFIG_MACH_T10_3G_OPEN)
  48. static void haptic_power_onoff(int onoff)
  49. {
  50. int ret;
  51. static struct regulator *reg_l6;
  52. if (!reg_l6) {
  53. reg_l6 = regulator_get(&vibrator_drvdata.client->dev,"vddo");
  54. if (IS_ERR(reg_l6)) {
  55. printk(KERN_ERR"could not get 8226_l6, rc = %ld\n",
  56. PTR_ERR(reg_l6));
  57. return;
  58. }
  59. ret = regulator_set_voltage(reg_l6, 1800000, 1800000);
  60. }
  61. if (onoff) {
  62. ret = regulator_enable(reg_l6);
  63. if (ret) {
  64. printk(KERN_ERR"enable l6 failed, rc=%d\n", ret);
  65. return;
  66. }
  67. printk(KERN_DEBUG"haptic power_on is finished.\n");
  68. } else {
  69. if (regulator_is_enabled(reg_l6)) {
  70. ret = regulator_disable(reg_l6);
  71. if (ret) {
  72. printk(KERN_ERR"disable l6 failed, rc=%d\n",
  73. ret);
  74. return;
  75. }
  76. }
  77. printk(KERN_DEBUG"haptic power_off is finished.\n");
  78. }
  79. }
  80. #endif
  81. static void vibe_set_pwm_freq(int intensity)
  82. {
  83. int32_t calc_d;
  84. /* Put the MND counter in reset mode for programming */
  85. HWIO_OUTM(GP1_CFG_RCGR, HWIO_GP_SRC_SEL_VAL_BMSK,0 << HWIO_GP_SRC_SEL_VAL_SHFT); //SRC_SEL = 000(cxo)
  86. #if defined (CONFIG_MACH_MATISSELTE_ATT)
  87. HWIO_OUTM(GP1_CFG_RCGR, HWIO_GP_SRC_DIV_VAL_BMSK,23 << HWIO_GP_SRC_DIV_VAL_SHFT); //SRC_DIV = 11111 (Div 12)
  88. #else
  89. #if defined (CONFIG_MACH_MATISSE3G_OPEN) || defined (CONFIG_SEC_MATISSELTE_COMMON)
  90. HWIO_OUTM(GP1_CFG_RCGR, HWIO_GP_SRC_DIV_VAL_BMSK,29 << HWIO_GP_SRC_DIV_VAL_SHFT); //SRC_DIV = 11111 (Div 16)
  91. #else
  92. HWIO_OUTM(GP1_CFG_RCGR, HWIO_GP_SRC_DIV_VAL_BMSK,31 << HWIO_GP_SRC_DIV_VAL_SHFT); //SRC_DIV = 11111 (Div 16)
  93. #endif
  94. #endif//CONFIG_MACH_MATISSELTE_ATT
  95. HWIO_OUTM(GP1_CFG_RCGR, HWIO_GP_MODE_VAL_BMSK,2 << HWIO_GP_MODE_VAL_SHFT); //Mode Select 10
  96. //M value
  97. HWIO_OUTM(GP_M_REG, HWIO_GP_MD_REG_M_VAL_BMSK,g_nlra_gp_clk_m << HWIO_GP_MD_REG_M_VAL_SHFT);
  98. if (intensity > 0){
  99. calc_d = g_nlra_gp_clk_n - (((intensity * g_nlra_gp_clk_pwm_mul) >> 8));
  100. calc_d = calc_d * motor_strength /100;
  101. if(calc_d < motor_min_strength)
  102. calc_d = motor_min_strength;
  103. }
  104. else {
  105. calc_d = ((intensity * g_nlra_gp_clk_pwm_mul) >> 8) + g_nlra_gp_clk_d;
  106. if(g_nlra_gp_clk_n - calc_d > g_nlra_gp_clk_n * motor_strength /100)
  107. calc_d = g_nlra_gp_clk_n - g_nlra_gp_clk_n * motor_strength /100;
  108. }
  109. // D value
  110. HWIO_OUTM(GP_D_REG, HWIO_GP_MD_REG_D_VAL_BMSK,(~((int16_t)calc_d << 1)) << HWIO_GP_MD_REG_D_VAL_SHFT);
  111. //N value
  112. HWIO_OUTM(GP_NS_REG, HWIO_GP_NS_REG_GP_N_VAL_BMSK,~(g_nlra_gp_clk_n - g_nlra_gp_clk_m) << 0);
  113. }
  114. static void vibe_pwm_onoff(u8 onoff)
  115. {
  116. if (onoff) {
  117. HWIO_OUTM(GP1_CMD_RCGR,HWIO_UPDATE_VAL_BMSK,
  118. 1 << HWIO_UPDATE_VAL_SHFT);//UPDATE ACTIVE
  119. HWIO_OUTM(GP1_CMD_RCGR,HWIO_ROOT_EN_VAL_BMSK,
  120. 1 << HWIO_ROOT_EN_VAL_SHFT);//ROOT_EN
  121. HWIO_OUTM(CAMSS_GP1_CBCR, HWIO_CLK_ENABLE_VAL_BMSK,
  122. 1 << HWIO_CLK_ENABLE_VAL_SHFT); //CLK_ENABLE
  123. } else {
  124. HWIO_OUTM(GP1_CMD_RCGR,HWIO_UPDATE_VAL_BMSK,
  125. 0 << HWIO_UPDATE_VAL_SHFT);
  126. HWIO_OUTM(GP1_CMD_RCGR,HWIO_ROOT_EN_VAL_BMSK,
  127. 0 << HWIO_ROOT_EN_VAL_SHFT);
  128. HWIO_OUTM(CAMSS_GP1_CBCR, HWIO_CLK_ENABLE_VAL_BMSK,
  129. 0 << HWIO_CLK_ENABLE_VAL_SHFT);
  130. }
  131. }
  132. static int vib_isa1200_onoff(u8 onoff)
  133. {
  134. if(onoff) {
  135. vibrator_write_register(HCTRL0, 0x88); //HCTRL0
  136. vibrator_write_register(HCTRL1, 0x4B);
  137. } else {
  138. vibrator_write_register(HCTRL0, 0x08);
  139. }
  140. return 0;
  141. }
  142. static void vibe_set_intensity(int intensity)
  143. {
  144. if (0 == intensity)
  145. vibe_pwm_onoff(0);
  146. else {
  147. if (MAX_INTENSITY == intensity)
  148. intensity = 1;
  149. else if (0 != intensity) {
  150. int tmp = MAX_INTENSITY - intensity;
  151. intensity = (tmp / 79); // 79 := 10000 / 127
  152. }
  153. vibe_set_pwm_freq(intensity);
  154. vibe_pwm_onoff(1);
  155. }
  156. }
  157. static void isa1200_initialize(void)
  158. {
  159. gpio_tlmm_config(GPIO_CFG(vibrator_drvdata.vib_clk,0, GPIO_CFG_OUTPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA),GPIO_CFG_ENABLE);
  160. msleep(1);
  161. vibrator_write_register(HCTRL0, 0x08);
  162. vibrator_write_register(HCTRL1, 0x4B);
  163. }
  164. static int set_vibrator(int timeout)
  165. {
  166. if(timeout) {
  167. #if defined (CONFIG_MACH_MATISSE3G_OPEN) || defined (CONFIG_SEC_MATISSELTE_COMMON) || defined (CONFIG_MACH_T10_3G_OPEN)
  168. vibrator_drvdata.power_onoff(1);
  169. gpio_tlmm_config(GPIO_CFG(vibrator_drvdata.vib_clk, 3, GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_2MA),GPIO_CFG_ENABLE);
  170. #else
  171. gpio_tlmm_config(GPIO_CFG(vibrator_drvdata.vib_clk, 6, GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_2MA),GPIO_CFG_ENABLE);
  172. #endif
  173. gpio_set_value(vibrator_drvdata.vib_clk, VIBRATION_ON);
  174. gpio_set_value(vibrator_drvdata.motor_en, VIBRATION_ON);
  175. #if defined (CONFIG_MACH_MATISSE3G_OPEN) || defined (CONFIG_SEC_MATISSELTE_COMMON)
  176. msleep(1);
  177. #endif
  178. vib_isa1200_onoff(1);
  179. } else {
  180. vib_isa1200_onoff(0);
  181. #if defined (CONFIG_MACH_MATISSE3G_OPEN) || defined (CONFIG_SEC_MATISSELTE_COMMON) || defined (CONFIG_MACH_T10_3G_OPEN)
  182. vibrator_drvdata.power_onoff(0);
  183. #endif
  184. gpio_set_value(vibrator_drvdata.motor_en, VIBRATION_OFF);
  185. gpio_tlmm_config(GPIO_CFG(vibrator_drvdata.vib_clk, 0, GPIO_CFG_OUTPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA),GPIO_CFG_ENABLE);
  186. gpio_set_value(vibrator_drvdata.vib_clk, VIBRATION_OFF);
  187. }
  188. vibrator_value = timeout;
  189. return 0;
  190. }
  191. static void _set_vibrator_work(struct work_struct *unused)
  192. {
  193. set_vibrator(vibrator_work);
  194. return;
  195. }
  196. static enum hrtimer_restart vibtation_timer_func(struct hrtimer *timer)
  197. {
  198. vibrator_work = 0;
  199. schedule_work(&vib_work);
  200. return HRTIMER_NORESTART;
  201. }
  202. static int get_time_for_vibtation(struct timed_output_dev *dev)
  203. {
  204. int remaining;
  205. if (hrtimer_active(&timer)) {
  206. ktime_t r = hrtimer_get_remaining(&timer);
  207. remaining = (int)ktime_to_ms(r);
  208. } else
  209. remaining = 0;
  210. if (vibrator_value == -1)
  211. remaining = -1;
  212. return remaining;
  213. }
  214. static void enable_vibtation_from_user(struct timed_output_dev *dev,int value)
  215. {
  216. printk("[VIB] : time = %d msec \n", value);
  217. hrtimer_cancel(&timer);
  218. /* set_vibrator(value); */
  219. vibrator_work = value;
  220. schedule_work(&vib_work);
  221. if (value > 0)
  222. {
  223. if (value > max_timeout)
  224. value = max_timeout;
  225. hrtimer_start(&timer,ktime_set(value / 1000, (value % 1000) * 1000000),
  226. HRTIMER_MODE_REL);
  227. vibrator_value = 0;
  228. }
  229. }
  230. static struct timed_output_dev timed_output_vt = {
  231. .name = "vibrator",
  232. .get_time = get_time_for_vibtation,
  233. .enable = enable_vibtation_from_user,
  234. };
  235. static ssize_t intensity_store(struct device *dev,
  236. struct device_attribute *devattr, const char *buf, size_t count)
  237. {
  238. int ret = 0, set_intensity = 0;
  239. ret = kstrtoint(buf, 0, &set_intensity);
  240. if ((set_intensity < 0) || (set_intensity > MAX_INTENSITY)) {
  241. pr_err("[VIB]: %sout of rage\n", __func__);
  242. return -EINVAL;
  243. }
  244. vibe_set_intensity(set_intensity);
  245. vibrator_drvdata.intensity = set_intensity;
  246. return count;
  247. }
  248. static ssize_t intensity_show(struct device *dev,
  249. struct device_attribute *attr, char *buf)
  250. {
  251. return sprintf(buf, "intensity: %u\n", vibrator_drvdata.intensity);
  252. }
  253. static DEVICE_ATTR(intensity, 0660, intensity_show, intensity_store);
  254. static void vibtation_start(void)
  255. {
  256. int ret = 0;
  257. hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  258. timer.function = vibtation_timer_func;
  259. ret = timed_output_dev_register(&timed_output_vt);
  260. if(ret)
  261. pr_info("[VIB]: timed_output_dev_register is fail\n");
  262. ret = sysfs_create_file(&timed_output_vt.dev->kobj, &dev_attr_intensity.attr);
  263. if (ret < 0) {
  264. pr_err("[VIB]: Failed to register sysfs intensity: %d\n", ret);
  265. }
  266. }
  267. /* Module info */
  268. int vibrator_write_register(u8 addr, u8 w_data)
  269. {
  270. if (i2c_smbus_write_byte_data(vibrator_drvdata.client, addr, w_data) < 0) {
  271. pr_err("%s: Failed to write addr=[0x%x], data=[0x%x]\n",
  272. __func__, addr, w_data);
  273. return -1;
  274. }
  275. return 0;
  276. }
  277. static int isa1200_parse_dt(struct device *dev)
  278. {
  279. struct device_node *np = dev->of_node;
  280. vibrator_drvdata.motor_en = of_get_named_gpio(np, "isa1200,motor_en",0);
  281. vibrator_drvdata.vib_clk = of_get_named_gpio(np, "isa1200,vib_clk",0);
  282. #if defined(CONFIG_MACH_T10_3G_OPEN)
  283. gpio_tlmm_config(GPIO_CFG(vibrator_drvdata.motor_en, 0, GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_2MA),GPIO_CFG_DISABLE);
  284. #endif
  285. return 0;
  286. }
  287. static int __devinit isa1200_vibrator_i2c_probe(struct i2c_client *client,
  288. const struct i2c_device_id *id)
  289. {
  290. int error; /* initialized below */
  291. pr_info("[VIB]: %s\n", __func__);
  292. motor_min_strength = g_nlra_gp_clk_n*MOTOR_MIN_STRENGTH/100;
  293. vibrator_drvdata.client = client;
  294. if(!(client->dev.of_node)){
  295. pr_info("[VIB]: %s failed, DT is NULL", __func__);
  296. return -ENODEV;
  297. }
  298. error = isa1200_parse_dt(&client->dev);
  299. if (error)
  300. return error;
  301. if( gpio_request(vibrator_drvdata.motor_en, "MOTOR_EN") < 0)
  302. {
  303. return -EINVAL;
  304. }
  305. gpio_direction_output(vibrator_drvdata.motor_en, 0);
  306. gpio_export(vibrator_drvdata.motor_en, 0);
  307. virt_mmss_gp1_base = ioremap(MSM_MMSS_GP1_BASE,0x28);
  308. if (!virt_mmss_gp1_base)
  309. panic("[VIB]: Unable to ioremap MSM_MMSS_GP1 memory!");
  310. #if defined(CONFIG_MACH_MATISSE3G_OPEN) || defined (CONFIG_SEC_MATISSELTE_COMMON) || defined (CONFIG_MACH_T10_3G_OPEN)
  311. vibrator_drvdata.power_onoff = haptic_power_onoff;
  312. #endif
  313. vibrator_drvdata.intensity = MAX_INTENSITY;
  314. isa1200_initialize();
  315. vibe_set_intensity(vibrator_drvdata.intensity);
  316. isa1200_enabled = 1;
  317. vibtation_start();
  318. return 0;
  319. }
  320. static int __devexit isa1200_remove(struct i2c_client *client)
  321. {
  322. pr_info("[VIB]: isa1200_remove_module.\n");
  323. iounmap(virt_mmss_gp1_base);
  324. gpio_free(vibrator_drvdata.motor_en);
  325. return 0;
  326. }
  327. static int isa1200_vibrator_suspend(struct i2c_client *client,pm_message_t mesg)
  328. {
  329. int ret = 0;
  330. if(isa1200_enabled){
  331. vibrator_write_register(0x30, 0x08);
  332. gpio_set_value(vibrator_drvdata.motor_en, VIBRATION_OFF);
  333. isa1200_enabled = 0;
  334. }
  335. return ret;
  336. }
  337. static int isa1200_vibrator_resume(struct i2c_client *client)
  338. {
  339. pr_info("[VIB]:isa1200_vibrator_resume\n");
  340. return 0;
  341. }
  342. static const struct i2c_device_id isa1200_vibrator_device_id[] = {
  343. {"isa1200_vibrator", 0},
  344. {}
  345. };
  346. MODULE_DEVICE_TABLE(i2c, isa1200_vibrator_device_id);
  347. static struct of_device_id isa1200_vibrator_table[] = {
  348. { .compatible = "isa1200_vibrator,vibrator",},
  349. { },
  350. };
  351. static struct i2c_driver isa1200_vibrator_i2c_driver = {
  352. .driver = {
  353. .name = "isa1200_vibrator",
  354. .owner = THIS_MODULE,
  355. .of_match_table = isa1200_vibrator_table,
  356. },
  357. .probe = isa1200_vibrator_i2c_probe,
  358. .id_table = isa1200_vibrator_device_id,
  359. .suspend = isa1200_vibrator_suspend,
  360. .resume = isa1200_vibrator_resume,
  361. .remove = __devexit_p(isa1200_remove),
  362. };
  363. static int __init isa1200_vibrator_init(void)
  364. {
  365. return(i2c_add_driver(&isa1200_vibrator_i2c_driver));
  366. }
  367. static void __exit isa1200_vibrator_exit(void)
  368. {
  369. i2c_del_driver(&isa1200_vibrator_i2c_driver);
  370. }
  371. module_init(isa1200_vibrator_init);
  372. module_exit(isa1200_vibrator_exit);
  373. MODULE_AUTHOR("Samsung Corporation");
  374. MODULE_DESCRIPTION("Vibrator driver for the isa1200 ic");
  375. MODULE_LICENSE("GPL v2");