blob: a58c7d696c187de6f143a3733df67f2cf4661e26 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * isa1200.c - Haptic Motor
3 *
4 * Copyright (C) 2009 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
Mohan Pallaka73787fa2011-09-09 15:14:20 +05306 * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07007 *
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
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/i2c.h>
16#include <linux/gpio.h>
17#include <linux/delay.h>
18#include <linux/err.h>
19#include <linux/pwm.h>
20#include <linux/workqueue.h>
21#include <linux/slab.h>
Mohan Pallaka73787fa2011-09-09 15:14:20 +053022#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023#include <linux/i2c/isa1200.h>
24#include "../staging/android/timed_output.h"
25
26#define ISA1200_HCTRL0 0x30
27#define ISA1200_HCTRL1 0x31
28#define ISA1200_HCTRL5 0x35
29
30#define ISA1200_HCTRL0_RESET 0x01
31#define ISA1200_HCTRL1_RESET 0x4B
32
33#define ISA1200_HCTRL5_VIB_STRT 0xD5
34#define ISA1200_HCTRL5_VIB_STOP 0x6B
35
36struct isa1200_chip {
37 struct i2c_client *client;
38 struct isa1200_platform_data *pdata;
39 struct pwm_device *pwm;
40 struct hrtimer timer;
41 struct timed_output_dev dev;
42 struct work_struct work;
43 spinlock_t lock;
44 unsigned int enable;
45 unsigned int period_ns;
Mohan Pallaka73787fa2011-09-09 15:14:20 +053046 bool is_len_gpio_valid;
47 struct regulator **regs;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048};
49
50static int isa1200_read_reg(struct i2c_client *client, int reg)
51{
52 int ret;
53
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
58 return ret;
59}
60
61static int isa1200_write_reg(struct i2c_client *client, int reg, u8 value)
62{
63 int ret;
64
65 ret = i2c_smbus_write_byte_data(client, reg, value);
66 if (ret < 0)
67 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
68
69 return ret;
70}
71
72static void isa1200_vib_set(struct isa1200_chip *haptic, int enable)
73{
74 int rc = 0;
75
76 if (enable) {
77 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
78 int period_us = haptic->period_ns / 1000;
79 rc = pwm_config(haptic->pwm,
80 (period_us * haptic->pdata->duty) / 100,
81 period_us);
82 if (rc < 0)
83 pr_err("%s: pwm_config fail\n", __func__);
84 rc = pwm_enable(haptic->pwm);
85 if (rc < 0)
86 pr_err("%s: pwm_enable fail\n", __func__);
87 } else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
88 rc = isa1200_write_reg(haptic->client,
89 ISA1200_HCTRL5,
90 ISA1200_HCTRL5_VIB_STRT);
91 if (rc < 0)
92 pr_err("%s: start vibartion fail\n", __func__);
93 }
94 } else {
95 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE)
96 pwm_disable(haptic->pwm);
97 else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
98 rc = isa1200_write_reg(haptic->client,
99 ISA1200_HCTRL5,
100 ISA1200_HCTRL5_VIB_STOP);
101 if (rc < 0)
102 pr_err("%s: stop vibartion fail\n", __func__);
103 }
104 }
105}
106
107static void isa1200_chip_work(struct work_struct *work)
108{
109 struct isa1200_chip *haptic;
110
111 haptic = container_of(work, struct isa1200_chip, work);
112 isa1200_vib_set(haptic, haptic->enable);
113}
114
115static void isa1200_chip_enable(struct timed_output_dev *dev, int value)
116{
117 struct isa1200_chip *haptic = container_of(dev, struct isa1200_chip,
118 dev);
119 unsigned long flags;
120
121 spin_lock_irqsave(&haptic->lock, flags);
122 hrtimer_cancel(&haptic->timer);
123 if (value == 0)
124 haptic->enable = 0;
125 else {
126 value = (value > haptic->pdata->max_timeout ?
127 haptic->pdata->max_timeout : value);
128 haptic->enable = 1;
129 hrtimer_start(&haptic->timer,
130 ktime_set(value / 1000, (value % 1000) * 1000000),
131 HRTIMER_MODE_REL);
132 }
133 spin_unlock_irqrestore(&haptic->lock, flags);
134 schedule_work(&haptic->work);
135}
136
137static int isa1200_chip_get_time(struct timed_output_dev *dev)
138{
139 struct isa1200_chip *haptic = container_of(dev, struct isa1200_chip,
140 dev);
141
142 if (hrtimer_active(&haptic->timer)) {
143 ktime_t r = hrtimer_get_remaining(&haptic->timer);
144 struct timeval t = ktime_to_timeval(r);
145 return t.tv_sec * 1000 + t.tv_usec / 1000;
146 } else
147 return 0;
148}
149
150static enum hrtimer_restart isa1200_vib_timer_func(struct hrtimer *timer)
151{
152 struct isa1200_chip *haptic = container_of(timer, struct isa1200_chip,
153 timer);
154 haptic->enable = 0;
155 schedule_work(&haptic->work);
156
157 return HRTIMER_NORESTART;
158}
159
160static void dump_isa1200_reg(char *str, struct i2c_client *client)
161{
162 pr_debug("%s reg0x%x=0x%x, reg0x%x=0x%x, reg0x%x=0x%x\n", str,
163 ISA1200_HCTRL0, isa1200_read_reg(client, ISA1200_HCTRL0),
164 ISA1200_HCTRL1, isa1200_read_reg(client, ISA1200_HCTRL1),
165 ISA1200_HCTRL5, isa1200_read_reg(client, ISA1200_HCTRL5));
166}
167
168static int isa1200_setup(struct i2c_client *client)
169{
170 struct isa1200_chip *haptic = i2c_get_clientdata(client);
171 int value, temp, rc;
172
173 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530174 if (haptic->is_len_gpio_valid == true)
175 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
176
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 udelay(250);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530178
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 1);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530180 if (haptic->is_len_gpio_valid == true)
181 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182
183 value = (haptic->pdata->smart_en << 3) |
184 (haptic->pdata->is_erm << 5) |
185 (haptic->pdata->ext_clk_en << 7);
186
187 rc = isa1200_write_reg(client, ISA1200_HCTRL1, value);
188 if (rc < 0) {
189 pr_err("%s: i2c write failure\n", __func__);
190 return rc;
191 }
192
193 if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
194 temp = haptic->pdata->pwm_fd.pwm_div;
195 if (temp < 128 || temp > 1024 || temp % 128) {
196 pr_err("%s: Invalid divider\n", __func__);
197 goto reset_hctrl1;
198 }
199 value = ((temp >> 7) - 1);
200 } else if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
201 temp = haptic->pdata->pwm_fd.pwm_freq;
202 if (temp < 22400 || temp > 172600 || temp % 22400) {
203 pr_err("%s: Invalid frequency\n", __func__);
204 goto reset_hctrl1;
205 }
206 value = ((temp / 22400) - 1);
207 haptic->period_ns = NSEC_PER_SEC / temp;
208 }
209
210 value |= (haptic->pdata->mode_ctrl << 3) |
211 (haptic->pdata->overdrive_high << 5) |
212 (haptic->pdata->overdrive_en << 5) |
213 (haptic->pdata->chip_en << 7);
214
215 rc = isa1200_write_reg(client, ISA1200_HCTRL0, value);
216 if (rc < 0) {
217 pr_err("%s: i2c write failure\n", __func__);
218 goto reset_hctrl1;
219 }
220
221 dump_isa1200_reg("new:", client);
222 return 0;
223
224reset_hctrl1:
225 i2c_smbus_write_byte_data(client, ISA1200_HCTRL1,
226 ISA1200_HCTRL1_RESET);
227 return rc;
228}
229
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530230static int isa1200_reg_power(struct isa1200_chip *haptic, bool on)
231{
232 const struct isa1200_regulator *reg_info =
233 haptic->pdata->regulator_info;
234 u8 i, num_reg = haptic->pdata->num_regulators;
235 int rc;
236
237 for (i = 0; i < num_reg; i++) {
238 rc = regulator_set_optimum_mode(haptic->regs[i],
239 on ? reg_info[i].load_uA : 0);
240 if (rc < 0) {
241 pr_err("%s: regulator_set_optimum_mode failed(%d)\n",
242 __func__, rc);
243 goto regs_fail;
244 }
245
246 rc = on ? regulator_enable(haptic->regs[i]) :
247 regulator_disable(haptic->regs[i]);
248 if (rc < 0) {
249 pr_err("%s: regulator %sable fail %d\n", __func__,
250 on ? "en" : "dis", rc);
251 regulator_set_optimum_mode(haptic->regs[i],
252 !on ? reg_info[i].load_uA : 0);
253 goto regs_fail;
254 }
255 }
256
257 return 0;
258
259regs_fail:
260 while (i--) {
261 regulator_set_optimum_mode(haptic->regs[i],
262 !on ? reg_info[i].load_uA : 0);
263 !on ? regulator_enable(haptic->regs[i]) :
264 regulator_disable(haptic->regs[i]);
265 }
266 return rc;
267}
268
269static int isa1200_reg_setup(struct isa1200_chip *haptic, bool on)
270{
271 const struct isa1200_regulator *reg_info =
272 haptic->pdata->regulator_info;
273 u8 i, num_reg = haptic->pdata->num_regulators;
274 int rc = 0;
275
276 /* put regulators */
277 if (on == false) {
278 i = num_reg;
279 goto put_regs;
280 }
281
282 haptic->regs = kzalloc(num_reg * sizeof(struct regulator *),
283 GFP_KERNEL);
284 if (!haptic->regs) {
285 pr_err("unable to allocate memory\n");
286 return -ENOMEM;
287 }
288
289 for (i = 0; i < num_reg; i++) {
290 haptic->regs[i] = regulator_get(&haptic->client->dev,
291 reg_info[i].name);
292 if (IS_ERR(haptic->regs[i])) {
293 rc = PTR_ERR(haptic->regs[i]);
294 pr_err("%s:regulator get failed(%d)\n", __func__, rc);
295 goto put_regs;
296 }
297
298 if (regulator_count_voltages(haptic->regs[i]) > 0) {
299 rc = regulator_set_voltage(haptic->regs[i],
300 reg_info[i].min_uV, reg_info[i].max_uV);
301 if (rc) {
302 pr_err("%s: regulator_set_voltage failed(%d)\n",
303 __func__, rc);
304 regulator_put(haptic->regs[i]);
305 goto put_regs;
306 }
307 }
308 }
309
310 return rc;
311
312put_regs:
313 while (i--) {
314 if (regulator_count_voltages(haptic->regs[i]) > 0)
315 regulator_set_voltage(haptic->regs[i], 0,
316 reg_info[i].max_uV);
317 regulator_put(haptic->regs[i]);
318 }
319 kfree(haptic->regs);
320 return rc;
321}
322
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323static int __devinit isa1200_probe(struct i2c_client *client,
324 const struct i2c_device_id *id)
325{
326 struct isa1200_chip *haptic;
327 struct isa1200_platform_data *pdata;
328 int ret;
329
330 if (!i2c_check_functionality(client->adapter,
331 I2C_FUNC_SMBUS_BYTE_DATA)) {
332 dev_err(&client->dev, "%s: no support for i2c read/write"
333 "byte data\n", __func__);
334 return -EIO;
335 }
336
337 pdata = client->dev.platform_data;
338 if (!pdata) {
339 dev_err(&client->dev, "%s: no platform data\n", __func__);
340 return -EINVAL;
341 }
342
343 if (pdata->dev_setup) {
344 ret = pdata->dev_setup(true);
345 if (ret < 0) {
346 dev_err(&client->dev, "dev setup failed\n");
347 return -EINVAL;
348 }
349 }
350
351 haptic = kzalloc(sizeof(struct isa1200_chip), GFP_KERNEL);
352 if (!haptic) {
353 ret = -ENOMEM;
354 goto mem_alloc_fail;
355 }
356 haptic->client = client;
357 haptic->enable = 0;
358 haptic->pdata = pdata;
359
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530360 if (pdata->regulator_info) {
361 ret = isa1200_reg_setup(haptic, true);
362 if (ret) {
363 dev_err(&client->dev, "%s: regulator setup failed\n",
364 __func__);
365 goto reg_setup_fail;
366 }
367
368 ret = isa1200_reg_power(haptic, true);
369 if (ret) {
370 dev_err(&client->dev, "%s: regulator power failed\n",
371 __func__);
372 goto reg_pwr_fail;
373 }
374 }
375
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376 if (pdata->power_on) {
377 ret = pdata->power_on(1);
378 if (ret) {
379 dev_err(&client->dev, "%s: power-up failed\n",
380 __func__);
381 goto pwr_up_fail;
382 }
383 }
384
385 spin_lock_init(&haptic->lock);
386 INIT_WORK(&haptic->work, isa1200_chip_work);
387
388 hrtimer_init(&haptic->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
389 haptic->timer.function = isa1200_vib_timer_func;
390
391 /*register with timed output class*/
392 haptic->dev.name = pdata->name;
393 haptic->dev.get_time = isa1200_chip_get_time;
394 haptic->dev.enable = isa1200_chip_enable;
395 ret = timed_output_dev_register(&haptic->dev);
396 if (ret < 0)
397 goto timed_reg_fail;
398
399 i2c_set_clientdata(client, haptic);
400
401 ret = gpio_is_valid(pdata->hap_en_gpio);
402 if (ret) {
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530403 ret = gpio_request(pdata->hap_en_gpio, "haptic_en_gpio");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404 if (ret) {
405 dev_err(&client->dev, "%s: gpio %d request failed\n",
406 __func__, pdata->hap_en_gpio);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530407 goto hen_gpio_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 }
409 } else {
410 dev_err(&client->dev, "%s: Invalid gpio %d\n", __func__,
411 pdata->hap_en_gpio);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530412 goto hen_gpio_fail;
413 }
414
415 haptic->is_len_gpio_valid = true;
416 ret = gpio_is_valid(haptic->pdata->hap_len_gpio);
417 if (ret) {
418 ret = gpio_request(pdata->hap_len_gpio,
419 "haptic_ldo_gpio");
420 if (ret) {
421 dev_err(&client->dev,
422 "%s: gpio %d request failed\n",
423 __func__, pdata->hap_len_gpio);
424 goto len_gpio_fail;
425 }
426 } else {
427 dev_err(&client->dev, "%s: gpio is not used/Invalid %d\n",
428 __func__, pdata->hap_len_gpio);
429 haptic->is_len_gpio_valid = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700430 }
431
432 ret = isa1200_setup(client);
433 if (ret) {
434 dev_err(&client->dev, "%s: setup fail %d\n", __func__, ret);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530435 goto setup_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436 }
437
438 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
439 haptic->pwm = pwm_request(pdata->pwm_ch_id, id->name);
440 if (IS_ERR(haptic->pwm)) {
441 dev_err(&client->dev, "%s: pwm request failed\n",
442 __func__);
443 ret = PTR_ERR(haptic->pwm);
444 goto reset_hctrl0;
445 }
446 }
447
448 printk(KERN_INFO "%s: %s registered\n", __func__, id->name);
449 return 0;
450
451reset_hctrl0:
452 i2c_smbus_write_byte_data(client, ISA1200_HCTRL0,
453 ISA1200_HCTRL0_RESET);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530454setup_fail:
455 if (haptic->is_len_gpio_valid == true)
456 gpio_free(pdata->hap_len_gpio);
457len_gpio_fail:
458 gpio_free(pdata->hap_en_gpio);
459hen_gpio_fail:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460 timed_output_dev_unregister(&haptic->dev);
461timed_reg_fail:
462 if (pdata->power_on)
463 pdata->power_on(0);
464pwr_up_fail:
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530465 if (pdata->regulator_info)
466 isa1200_reg_power(haptic, false);
467reg_pwr_fail:
468 if (pdata->regulator_info)
469 isa1200_reg_setup(haptic, false);
470reg_setup_fail:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 kfree(haptic);
472mem_alloc_fail:
473 if (pdata->dev_setup)
474 pdata->dev_setup(false);
475 return ret;
476}
477
478static int __devexit isa1200_remove(struct i2c_client *client)
479{
480 struct isa1200_chip *haptic = i2c_get_clientdata(client);
481
482 hrtimer_cancel(&haptic->timer);
483 cancel_work_sync(&haptic->work);
484
485 /* turn-off current vibration */
486 isa1200_vib_set(haptic, 0);
487
488 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE)
489 pwm_free(haptic->pwm);
490
491 timed_output_dev_unregister(&haptic->dev);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530492
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 gpio_free(haptic->pdata->hap_en_gpio);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530494 if (haptic->is_len_gpio_valid == true)
495 gpio_free(haptic->pdata->hap_len_gpio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496
497 /* reset hardware registers */
498 i2c_smbus_write_byte_data(client, ISA1200_HCTRL0,
499 ISA1200_HCTRL0_RESET);
500 i2c_smbus_write_byte_data(client, ISA1200_HCTRL1,
501 ISA1200_HCTRL1_RESET);
502
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503
504 /* power-off the chip */
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530505 if (haptic->pdata->regulator_info) {
506 isa1200_reg_power(haptic, false);
507 isa1200_reg_setup(haptic, false);
508 }
509
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 if (haptic->pdata->power_on)
511 haptic->pdata->power_on(0);
512
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530513 if (haptic->pdata->dev_setup)
514 haptic->pdata->dev_setup(false);
515
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516 kfree(haptic);
517 return 0;
518}
519
520#ifdef CONFIG_PM
521static int isa1200_suspend(struct i2c_client *client, pm_message_t mesg)
522{
523 struct isa1200_chip *haptic = i2c_get_clientdata(client);
524 int ret;
525
526 hrtimer_cancel(&haptic->timer);
527 cancel_work_sync(&haptic->work);
528 /* turn-off current vibration */
529 isa1200_vib_set(haptic, 0);
530
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530531 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
532 if (haptic->is_len_gpio_valid == true)
533 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
534
535 if (haptic->pdata->regulator_info)
536 isa1200_reg_power(haptic, false);
537
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538 if (haptic->pdata->power_on) {
539 ret = haptic->pdata->power_on(0);
540 if (ret) {
541 dev_err(&client->dev, "power-down failed\n");
542 return ret;
543 }
544 }
545
546 return 0;
547}
548
549static int isa1200_resume(struct i2c_client *client)
550{
551 struct isa1200_chip *haptic = i2c_get_clientdata(client);
552 int ret;
553
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530554 if (haptic->pdata->regulator_info)
555 isa1200_reg_power(haptic, true);
556
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 if (haptic->pdata->power_on) {
558 ret = haptic->pdata->power_on(1);
559 if (ret) {
560 dev_err(&client->dev, "power-up failed\n");
561 return ret;
562 }
563 }
564
565 isa1200_setup(client);
566 return 0;
567}
568#else
569#define isa1200_suspend NULL
570#define isa1200_resume NULL
571#endif
572
573static const struct i2c_device_id isa1200_id[] = {
574 { "isa1200_1", 0 },
575 { },
576};
577MODULE_DEVICE_TABLE(i2c, isa1200_id);
578
579static struct i2c_driver isa1200_driver = {
580 .driver = {
581 .name = "isa1200",
582 },
583 .probe = isa1200_probe,
584 .remove = __devexit_p(isa1200_remove),
585 .suspend = isa1200_suspend,
586 .resume = isa1200_resume,
587 .id_table = isa1200_id,
588};
589
590static int __init isa1200_init(void)
591{
592 return i2c_add_driver(&isa1200_driver);
593}
594
595static void __exit isa1200_exit(void)
596{
597 i2c_del_driver(&isa1200_driver);
598}
599
600module_init(isa1200_init);
601module_exit(isa1200_exit);
602
603MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
604MODULE_DESCRIPTION("ISA1200 Haptic Motor driver");
605MODULE_LICENSE("GPL");