blob: c6d08d126e24a8d71cc0d98f236044d2edceeb30 [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>
Amy Maloche62158232012-12-07 18:40:04 -08006 * Copyright (c) 2010-2013, The Linux Foundation. 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>
Mohan Pallaka32f20a72012-06-14 14:41:11 +053023#include <linux/clk.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <linux/i2c/isa1200.h>
25#include "../staging/android/timed_output.h"
Amy Malochee3f29012012-12-07 18:40:04 -080026#include <linux/of_gpio.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027
28#define ISA1200_HCTRL0 0x30
29#define ISA1200_HCTRL1 0x31
30#define ISA1200_HCTRL5 0x35
31
32#define ISA1200_HCTRL0_RESET 0x01
33#define ISA1200_HCTRL1_RESET 0x4B
34
35#define ISA1200_HCTRL5_VIB_STRT 0xD5
36#define ISA1200_HCTRL5_VIB_STOP 0x6B
Mohan Pallakaace9fa92012-05-11 13:05:40 +053037#define ISA1200_POWER_DOWN_MASK 0x7F
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038
39struct isa1200_chip {
40 struct i2c_client *client;
41 struct isa1200_platform_data *pdata;
42 struct pwm_device *pwm;
43 struct hrtimer timer;
44 struct timed_output_dev dev;
45 struct work_struct work;
Mohan Pallaka65206ee2012-12-05 16:50:48 +053046 struct mutex lock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047 unsigned int enable;
48 unsigned int period_ns;
Mohan Pallaka73787fa2011-09-09 15:14:20 +053049 bool is_len_gpio_valid;
50 struct regulator **regs;
Mohan Pallakaace9fa92012-05-11 13:05:40 +053051 bool clk_on;
52 u8 hctrl0_val;
Mohan Pallaka32f20a72012-06-14 14:41:11 +053053 struct clk *pwm_clk;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054};
55
56static int isa1200_read_reg(struct i2c_client *client, int reg)
57{
58 int ret;
59
60 ret = i2c_smbus_read_byte_data(client, reg);
61 if (ret < 0)
62 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
63
64 return ret;
65}
66
67static int isa1200_write_reg(struct i2c_client *client, int reg, u8 value)
68{
69 int ret;
70
71 ret = i2c_smbus_write_byte_data(client, reg, value);
72 if (ret < 0)
73 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
74
75 return ret;
76}
77
78static void isa1200_vib_set(struct isa1200_chip *haptic, int enable)
79{
80 int rc = 0;
81
82 if (enable) {
Mohan Pallakaace9fa92012-05-11 13:05:40 +053083 /* if hen and len are seperate then enable hen
84 * otherwise set normal mode bit */
85 if (haptic->is_len_gpio_valid == true)
86 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 1);
87 else {
88 rc = isa1200_write_reg(haptic->client, ISA1200_HCTRL0,
89 haptic->hctrl0_val | ~ISA1200_POWER_DOWN_MASK);
90 if (rc < 0) {
91 pr_err("%s: i2c write failure\n", __func__);
92 return;
93 }
94 }
95
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
97 int period_us = haptic->period_ns / 1000;
Mohan Pallakaace9fa92012-05-11 13:05:40 +053098
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 rc = pwm_config(haptic->pwm,
100 (period_us * haptic->pdata->duty) / 100,
101 period_us);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530102 if (rc < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103 pr_err("%s: pwm_config fail\n", __func__);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530104 goto chip_dwn;
105 }
106
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 rc = pwm_enable(haptic->pwm);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530108 if (rc < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109 pr_err("%s: pwm_enable fail\n", __func__);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530110 goto chip_dwn;
111 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 } else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530113 /* check for board specific clk callback */
114 if (haptic->pdata->clk_enable) {
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530115 rc = haptic->pdata->clk_enable(true);
116 if (rc < 0) {
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530117 pr_err("%s: clk enable cb failed\n",
118 __func__);
119 goto chip_dwn;
120 }
121 }
122
123 /* vote for clock */
124 if (haptic->pdata->need_pwm_clk && !haptic->clk_on) {
Amy Malochee3f29012012-12-07 18:40:04 -0800125 rc = clk_prepare_enable(haptic->pwm_clk);
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530126 if (rc < 0) {
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530127 pr_err("%s: clk enable failed\n",
128 __func__);
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530129 goto dis_clk_cb;
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530130 }
131 haptic->clk_on = true;
132 }
133
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134 rc = isa1200_write_reg(haptic->client,
135 ISA1200_HCTRL5,
136 ISA1200_HCTRL5_VIB_STRT);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530137 if (rc < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138 pr_err("%s: start vibartion fail\n", __func__);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530139 goto dis_clk;
140 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141 }
142 } else {
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530143 /* if hen and len are seperate then pull down hen
144 * otherwise set power down bit */
145 if (haptic->is_len_gpio_valid == true)
146 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
147 else {
148 rc = isa1200_write_reg(haptic->client, ISA1200_HCTRL0,
149 haptic->hctrl0_val & ISA1200_POWER_DOWN_MASK);
150 if (rc < 0) {
151 pr_err("%s: i2c write failure\n", __func__);
152 return;
153 }
154 }
155
156 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700157 pwm_disable(haptic->pwm);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530158 } else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159 rc = isa1200_write_reg(haptic->client,
160 ISA1200_HCTRL5,
161 ISA1200_HCTRL5_VIB_STOP);
162 if (rc < 0)
163 pr_err("%s: stop vibartion fail\n", __func__);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530164
165 /* de-vote clock */
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530166 if (haptic->pdata->need_pwm_clk && haptic->clk_on) {
Amy Maloche62158232012-12-07 18:40:04 -0800167 clk_disable_unprepare(haptic->pwm_clk);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530168 haptic->clk_on = false;
169 }
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530170 /* check for board specific clk callback */
171 if (haptic->pdata->clk_enable) {
172 rc = haptic->pdata->clk_enable(false);
173 if (rc < 0)
174 pr_err("%s: clk disable cb failed\n",
175 __func__);
176 }
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530177 }
178 }
179
180 return;
181
182dis_clk:
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530183 if (haptic->pdata->need_pwm_clk && haptic->clk_on) {
Amy Maloche62158232012-12-07 18:40:04 -0800184 clk_disable_unprepare(haptic->pwm_clk);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530185 haptic->clk_on = false;
186 }
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530187
188dis_clk_cb:
189 if (haptic->pdata->clk_enable) {
190 rc = haptic->pdata->clk_enable(false);
191 if (rc < 0)
192 pr_err("%s: clk disable cb failed\n", __func__);
193 }
194
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530195chip_dwn:
196 if (haptic->is_len_gpio_valid == true)
197 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
198 else {
199 rc = isa1200_write_reg(haptic->client, ISA1200_HCTRL0,
200 haptic->hctrl0_val & ISA1200_POWER_DOWN_MASK);
201 if (rc < 0) {
202 pr_err("%s: i2c write failure\n", __func__);
203 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204 }
205 }
206}
207
208static void isa1200_chip_work(struct work_struct *work)
209{
210 struct isa1200_chip *haptic;
211
212 haptic = container_of(work, struct isa1200_chip, work);
213 isa1200_vib_set(haptic, haptic->enable);
214}
215
216static void isa1200_chip_enable(struct timed_output_dev *dev, int value)
217{
218 struct isa1200_chip *haptic = container_of(dev, struct isa1200_chip,
219 dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220
Mohan Pallaka65206ee2012-12-05 16:50:48 +0530221 mutex_lock(&haptic->lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222 hrtimer_cancel(&haptic->timer);
223 if (value == 0)
224 haptic->enable = 0;
225 else {
226 value = (value > haptic->pdata->max_timeout ?
227 haptic->pdata->max_timeout : value);
228 haptic->enable = 1;
229 hrtimer_start(&haptic->timer,
230 ktime_set(value / 1000, (value % 1000) * 1000000),
231 HRTIMER_MODE_REL);
232 }
Mohan Pallaka65206ee2012-12-05 16:50:48 +0530233 mutex_unlock(&haptic->lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 schedule_work(&haptic->work);
235}
236
237static int isa1200_chip_get_time(struct timed_output_dev *dev)
238{
239 struct isa1200_chip *haptic = container_of(dev, struct isa1200_chip,
240 dev);
241
242 if (hrtimer_active(&haptic->timer)) {
243 ktime_t r = hrtimer_get_remaining(&haptic->timer);
244 struct timeval t = ktime_to_timeval(r);
245 return t.tv_sec * 1000 + t.tv_usec / 1000;
246 } else
247 return 0;
248}
249
250static enum hrtimer_restart isa1200_vib_timer_func(struct hrtimer *timer)
251{
252 struct isa1200_chip *haptic = container_of(timer, struct isa1200_chip,
253 timer);
254 haptic->enable = 0;
255 schedule_work(&haptic->work);
256
257 return HRTIMER_NORESTART;
258}
259
260static void dump_isa1200_reg(char *str, struct i2c_client *client)
261{
262 pr_debug("%s reg0x%x=0x%x, reg0x%x=0x%x, reg0x%x=0x%x\n", str,
263 ISA1200_HCTRL0, isa1200_read_reg(client, ISA1200_HCTRL0),
264 ISA1200_HCTRL1, isa1200_read_reg(client, ISA1200_HCTRL1),
265 ISA1200_HCTRL5, isa1200_read_reg(client, ISA1200_HCTRL5));
266}
267
268static int isa1200_setup(struct i2c_client *client)
269{
270 struct isa1200_chip *haptic = i2c_get_clientdata(client);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530271 int temp, rc;
272 u8 value;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700273
274 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530275 if (haptic->is_len_gpio_valid == true)
276 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
277
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278 udelay(250);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530279
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 1);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530281 if (haptic->is_len_gpio_valid == true)
282 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283
284 value = (haptic->pdata->smart_en << 3) |
285 (haptic->pdata->is_erm << 5) |
286 (haptic->pdata->ext_clk_en << 7);
287
288 rc = isa1200_write_reg(client, ISA1200_HCTRL1, value);
289 if (rc < 0) {
290 pr_err("%s: i2c write failure\n", __func__);
Mohan Pallakaacdca0f2011-11-21 13:08:00 +0530291 goto reset_gpios;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 }
293
294 if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) {
295 temp = haptic->pdata->pwm_fd.pwm_div;
296 if (temp < 128 || temp > 1024 || temp % 128) {
297 pr_err("%s: Invalid divider\n", __func__);
298 goto reset_hctrl1;
299 }
300 value = ((temp >> 7) - 1);
301 } else if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
302 temp = haptic->pdata->pwm_fd.pwm_freq;
303 if (temp < 22400 || temp > 172600 || temp % 22400) {
304 pr_err("%s: Invalid frequency\n", __func__);
305 goto reset_hctrl1;
306 }
307 value = ((temp / 22400) - 1);
308 haptic->period_ns = NSEC_PER_SEC / temp;
309 }
310
311 value |= (haptic->pdata->mode_ctrl << 3) |
312 (haptic->pdata->overdrive_high << 5) |
313 (haptic->pdata->overdrive_en << 5) |
314 (haptic->pdata->chip_en << 7);
315
316 rc = isa1200_write_reg(client, ISA1200_HCTRL0, value);
317 if (rc < 0) {
318 pr_err("%s: i2c write failure\n", __func__);
319 goto reset_hctrl1;
320 }
321
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530322 /* if hen and len are seperate then pull down hen
323 * otherwise set power down bit */
324 if (haptic->is_len_gpio_valid == true)
325 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
326 else {
327 rc = isa1200_write_reg(client, ISA1200_HCTRL0,
328 value & ISA1200_POWER_DOWN_MASK);
329 if (rc < 0) {
330 pr_err("%s: i2c write failure\n", __func__);
331 goto reset_hctrl1;
332 }
333 }
334
335 haptic->hctrl0_val = value;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700336 dump_isa1200_reg("new:", client);
337 return 0;
338
339reset_hctrl1:
340 i2c_smbus_write_byte_data(client, ISA1200_HCTRL1,
341 ISA1200_HCTRL1_RESET);
Mohan Pallakaacdca0f2011-11-21 13:08:00 +0530342reset_gpios:
343 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
344 if (haptic->is_len_gpio_valid == true)
345 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700346 return rc;
347}
348
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530349static int isa1200_reg_power(struct isa1200_chip *haptic, bool on)
350{
351 const struct isa1200_regulator *reg_info =
352 haptic->pdata->regulator_info;
353 u8 i, num_reg = haptic->pdata->num_regulators;
354 int rc;
355
356 for (i = 0; i < num_reg; i++) {
357 rc = regulator_set_optimum_mode(haptic->regs[i],
358 on ? reg_info[i].load_uA : 0);
359 if (rc < 0) {
360 pr_err("%s: regulator_set_optimum_mode failed(%d)\n",
361 __func__, rc);
362 goto regs_fail;
363 }
364
365 rc = on ? regulator_enable(haptic->regs[i]) :
366 regulator_disable(haptic->regs[i]);
367 if (rc < 0) {
368 pr_err("%s: regulator %sable fail %d\n", __func__,
369 on ? "en" : "dis", rc);
370 regulator_set_optimum_mode(haptic->regs[i],
371 !on ? reg_info[i].load_uA : 0);
372 goto regs_fail;
373 }
374 }
375
376 return 0;
377
378regs_fail:
379 while (i--) {
380 regulator_set_optimum_mode(haptic->regs[i],
381 !on ? reg_info[i].load_uA : 0);
382 !on ? regulator_enable(haptic->regs[i]) :
383 regulator_disable(haptic->regs[i]);
384 }
385 return rc;
386}
387
388static int isa1200_reg_setup(struct isa1200_chip *haptic, bool on)
389{
390 const struct isa1200_regulator *reg_info =
391 haptic->pdata->regulator_info;
392 u8 i, num_reg = haptic->pdata->num_regulators;
393 int rc = 0;
394
395 /* put regulators */
396 if (on == false) {
397 i = num_reg;
398 goto put_regs;
399 }
400
401 haptic->regs = kzalloc(num_reg * sizeof(struct regulator *),
402 GFP_KERNEL);
403 if (!haptic->regs) {
404 pr_err("unable to allocate memory\n");
405 return -ENOMEM;
406 }
407
408 for (i = 0; i < num_reg; i++) {
409 haptic->regs[i] = regulator_get(&haptic->client->dev,
Amy Malochee3f29012012-12-07 18:40:04 -0800410 reg_info[i].name);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530411 if (IS_ERR(haptic->regs[i])) {
412 rc = PTR_ERR(haptic->regs[i]);
Amy Malochee3f29012012-12-07 18:40:04 -0800413 pr_err("%s:regulator get failed(%d)\n", __func__, rc);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530414 goto put_regs;
415 }
416
417 if (regulator_count_voltages(haptic->regs[i]) > 0) {
418 rc = regulator_set_voltage(haptic->regs[i],
419 reg_info[i].min_uV, reg_info[i].max_uV);
420 if (rc) {
421 pr_err("%s: regulator_set_voltage failed(%d)\n",
422 __func__, rc);
423 regulator_put(haptic->regs[i]);
424 goto put_regs;
425 }
426 }
427 }
428
429 return rc;
430
431put_regs:
432 while (i--) {
433 if (regulator_count_voltages(haptic->regs[i]) > 0)
434 regulator_set_voltage(haptic->regs[i], 0,
435 reg_info[i].max_uV);
436 regulator_put(haptic->regs[i]);
437 }
438 kfree(haptic->regs);
439 return rc;
440}
441
Amy Malochee3f29012012-12-07 18:40:04 -0800442#ifdef CONFIG_OF
443static int isa1200_parse_dt(struct device *dev,
444 struct isa1200_platform_data *pdata)
445{
446 struct device_node *temp, *np = dev->of_node;
447 struct isa1200_regulator *reg_info;
448 enum of_gpio_flags hap_en_flags = OF_GPIO_ACTIVE_LOW;
449 enum of_gpio_flags hap_len_flags = OF_GPIO_ACTIVE_LOW;
450 int rc = 0;
451 u32 temp_val;
452 const char *temp_string;
453
454 rc = of_property_read_string(np, "label", &pdata->name);
455 if (rc) {
456 dev_err(dev, "Unable to read device name\n");
457 return rc;
458 }
459
460 pdata->chip_en = of_property_read_bool(np, "imagis,chip-en");
461 pdata->ext_clk_en = of_property_read_bool(np, "imagis,ext-clk-en");
462 pdata->is_erm = of_property_read_bool(np, "imagis,is-erm");
463 pdata->overdrive_high =
464 of_property_read_bool(np, "imagis,overdrive-high");
465 pdata->overdrive_en = of_property_read_bool(np, "imagis,overdrive-en");
466 pdata->smart_en = of_property_read_bool(np, "imagis,smart-en");
467 pdata->need_pwm_clk = of_property_read_bool(np, "imagis,need-pwm-clk");
468
469 pdata->hap_en_gpio = of_get_named_gpio_flags(np,
470 "imagis,hap-en-gpio", 0, &hap_en_flags);
471 pdata->hap_len_gpio = of_get_named_gpio_flags(np,
472 "imagis,hap-len-gpio", 0, &hap_len_flags);
473
474 rc = of_property_read_u32(np, "imagis,max-timeout",
475 &pdata->max_timeout);
476 if (rc) {
477 dev_err(dev, "Unable to read max timeout\n");
478 return rc;
479 }
480
481 rc = of_property_read_u32(np, "imagis,pwm-div", &pdata->pwm_fd.pwm_div);
482 if (rc && (rc != -EINVAL)) {
483 dev_err(dev, "Unable to read pwm division\n");
484 return rc;
485 }
486
487 rc = of_property_read_u32(np, "imagis,pwm-freq",
488 &pdata->pwm_fd.pwm_freq);
489 if (rc && (rc != -EINVAL)) {
490 dev_err(dev, "Unable to read pwm frequency\n");
491 return rc;
492 }
493
494 rc = of_property_read_u32(np, "imagis,pwm-ch-id", &pdata->pwm_ch_id);
495 if (rc && (rc != -EINVAL)) {
496 dev_err(dev, "Unable to read pwm channel id\n");
497 return rc;
498 }
499
500 rc = of_property_read_u32(np, "imagis,mode-ctrl", &pdata->mode_ctrl);
501 if (rc) {
502 dev_err(dev, "Unable to read control mode\n");
503 return rc;
504 }
505
506 rc = of_property_read_u32(np, "imagis,duty", &pdata->duty);
507 if (rc && (rc != -EINVAL)) {
508 dev_err(dev, "Unable to read duty cycle\n");
509 return rc;
510 }
511
512 pdata->num_regulators = 0;
513 temp = NULL;
514 while ((temp = of_get_next_child(np, temp)))
515 pdata->num_regulators++;
516
517 if (!pdata->num_regulators)
518 return 0;
519
520 reg_info = devm_kzalloc(dev, pdata->num_regulators *
521 sizeof(struct isa1200_regulator), GFP_KERNEL);
522 if (!reg_info)
523 return -ENOMEM;
524
525 pdata->regulator_info = reg_info;
526
527 for_each_child_of_node(np, temp) {
528 rc = of_property_read_string(temp,
529 "regulator-name", &temp_string);
530 if (rc) {
531 dev_err(dev, "Unable to read regulator name\n");
532 return rc;
533 } else
534 reg_info->name = temp_string;
535
536 rc = of_property_read_u32(temp, "regulator-max-microvolt",
537 &temp_val);
538 if (rc) {
539 dev_err(dev, "Unable to read max uV\n");
540 return rc;
541 } else
542 reg_info->max_uV = temp_val;
543
544 rc = of_property_read_u32(temp, "regulator-min-microvolt",
545 &temp_val);
546 if (rc) {
547 dev_err(dev, "Unable to read min uV\n");
548 return rc;
549 } else
550 reg_info->min_uV = temp_val;
551
552 rc = of_property_read_u32(temp, "regulator-max-microamp",
553 &temp_val);
554 if (rc) {
555 dev_err(dev, "Unable to read load uA\n");
556 return rc;
557 } else
558 reg_info->load_uA = temp_val;
559
560 reg_info++;
561 }
562
563 return 0;
564}
565#else
566static int isa1200_parse_dt(struct device *dev,
567 struct isa1200_platform_data *pdata)
568{
569 return -ENODEV;
570}
571#endif
572
573
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574static int __devinit isa1200_probe(struct i2c_client *client,
575 const struct i2c_device_id *id)
576{
577 struct isa1200_chip *haptic;
578 struct isa1200_platform_data *pdata;
579 int ret;
580
581 if (!i2c_check_functionality(client->adapter,
582 I2C_FUNC_SMBUS_BYTE_DATA)) {
583 dev_err(&client->dev, "%s: no support for i2c read/write"
584 "byte data\n", __func__);
585 return -EIO;
586 }
587
Amy Malochee3f29012012-12-07 18:40:04 -0800588 if (client->dev.of_node) {
589 pdata = devm_kzalloc(&client->dev,
590 sizeof(struct isa1200_platform_data), GFP_KERNEL);
591 if (!pdata) {
592 dev_err(&client->dev, "Failed to allocate memory\n");
593 return -ENOMEM;
594 }
595
596 ret = isa1200_parse_dt(&client->dev, pdata);
597 if (ret) {
598 dev_err(&client->dev, "Parsing DT failed(%d)", ret);
599 return ret;
600 }
601 } else
602 pdata = client->dev.platform_data;
603
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604 if (!pdata) {
605 dev_err(&client->dev, "%s: no platform data\n", __func__);
606 return -EINVAL;
607 }
608
609 if (pdata->dev_setup) {
610 ret = pdata->dev_setup(true);
611 if (ret < 0) {
612 dev_err(&client->dev, "dev setup failed\n");
613 return -EINVAL;
614 }
615 }
616
617 haptic = kzalloc(sizeof(struct isa1200_chip), GFP_KERNEL);
618 if (!haptic) {
619 ret = -ENOMEM;
620 goto mem_alloc_fail;
621 }
622 haptic->client = client;
623 haptic->enable = 0;
624 haptic->pdata = pdata;
625
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530626 if (pdata->regulator_info) {
627 ret = isa1200_reg_setup(haptic, true);
628 if (ret) {
629 dev_err(&client->dev, "%s: regulator setup failed\n",
630 __func__);
631 goto reg_setup_fail;
632 }
633
634 ret = isa1200_reg_power(haptic, true);
635 if (ret) {
636 dev_err(&client->dev, "%s: regulator power failed\n",
637 __func__);
638 goto reg_pwr_fail;
639 }
640 }
641
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700642 if (pdata->power_on) {
643 ret = pdata->power_on(1);
644 if (ret) {
645 dev_err(&client->dev, "%s: power-up failed\n",
646 __func__);
647 goto pwr_up_fail;
648 }
649 }
650
Mohan Pallaka65206ee2012-12-05 16:50:48 +0530651 mutex_init(&haptic->lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652 INIT_WORK(&haptic->work, isa1200_chip_work);
Mohan Pallakaace9fa92012-05-11 13:05:40 +0530653 haptic->clk_on = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654
655 hrtimer_init(&haptic->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
656 haptic->timer.function = isa1200_vib_timer_func;
657
658 /*register with timed output class*/
659 haptic->dev.name = pdata->name;
660 haptic->dev.get_time = isa1200_chip_get_time;
661 haptic->dev.enable = isa1200_chip_enable;
662 ret = timed_output_dev_register(&haptic->dev);
663 if (ret < 0)
664 goto timed_reg_fail;
665
666 i2c_set_clientdata(client, haptic);
667
668 ret = gpio_is_valid(pdata->hap_en_gpio);
669 if (ret) {
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530670 ret = gpio_request(pdata->hap_en_gpio, "haptic_en_gpio");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700671 if (ret) {
672 dev_err(&client->dev, "%s: gpio %d request failed\n",
673 __func__, pdata->hap_en_gpio);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530674 goto hen_gpio_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675 }
676 } else {
677 dev_err(&client->dev, "%s: Invalid gpio %d\n", __func__,
678 pdata->hap_en_gpio);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530679 goto hen_gpio_fail;
680 }
681
682 haptic->is_len_gpio_valid = true;
683 ret = gpio_is_valid(haptic->pdata->hap_len_gpio);
684 if (ret) {
685 ret = gpio_request(pdata->hap_len_gpio,
686 "haptic_ldo_gpio");
687 if (ret) {
688 dev_err(&client->dev,
689 "%s: gpio %d request failed\n",
690 __func__, pdata->hap_len_gpio);
691 goto len_gpio_fail;
692 }
693 } else {
694 dev_err(&client->dev, "%s: gpio is not used/Invalid %d\n",
695 __func__, pdata->hap_len_gpio);
696 haptic->is_len_gpio_valid = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700697 }
698
699 ret = isa1200_setup(client);
700 if (ret) {
701 dev_err(&client->dev, "%s: setup fail %d\n", __func__, ret);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530702 goto setup_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703 }
704
705 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) {
706 haptic->pwm = pwm_request(pdata->pwm_ch_id, id->name);
707 if (IS_ERR(haptic->pwm)) {
708 dev_err(&client->dev, "%s: pwm request failed\n",
709 __func__);
710 ret = PTR_ERR(haptic->pwm);
711 goto reset_hctrl0;
712 }
Mohan Pallaka32f20a72012-06-14 14:41:11 +0530713 } else if (haptic->pdata->need_pwm_clk) {
714 haptic->pwm_clk = clk_get(&client->dev, "pwm_clk");
715 if (IS_ERR(haptic->pwm_clk)) {
716 dev_err(&client->dev, "pwm_clk get failed\n");
717 ret = PTR_ERR(haptic->pwm_clk);
718 goto reset_hctrl0;
719 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 }
721
722 printk(KERN_INFO "%s: %s registered\n", __func__, id->name);
723 return 0;
724
725reset_hctrl0:
Mohan Pallakaacdca0f2011-11-21 13:08:00 +0530726 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
727 if (haptic->is_len_gpio_valid == true)
728 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
729 i2c_smbus_write_byte_data(client, ISA1200_HCTRL1,
730 ISA1200_HCTRL1_RESET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700731 i2c_smbus_write_byte_data(client, ISA1200_HCTRL0,
732 ISA1200_HCTRL0_RESET);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530733setup_fail:
734 if (haptic->is_len_gpio_valid == true)
735 gpio_free(pdata->hap_len_gpio);
736len_gpio_fail:
737 gpio_free(pdata->hap_en_gpio);
738hen_gpio_fail:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739 timed_output_dev_unregister(&haptic->dev);
740timed_reg_fail:
Mohan Pallaka65206ee2012-12-05 16:50:48 +0530741 mutex_destroy(&haptic->lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742 if (pdata->power_on)
743 pdata->power_on(0);
744pwr_up_fail:
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530745 if (pdata->regulator_info)
746 isa1200_reg_power(haptic, false);
747reg_pwr_fail:
748 if (pdata->regulator_info)
749 isa1200_reg_setup(haptic, false);
750reg_setup_fail:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751 kfree(haptic);
752mem_alloc_fail:
753 if (pdata->dev_setup)
754 pdata->dev_setup(false);
755 return ret;
756}
757
758static int __devexit isa1200_remove(struct i2c_client *client)
759{
760 struct isa1200_chip *haptic = i2c_get_clientdata(client);
761
762 hrtimer_cancel(&haptic->timer);
763 cancel_work_sync(&haptic->work);
764
765 /* turn-off current vibration */
766 isa1200_vib_set(haptic, 0);
767
768 if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE)
769 pwm_free(haptic->pwm);
770
771 timed_output_dev_unregister(&haptic->dev);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530772
Mohan Pallakaacdca0f2011-11-21 13:08:00 +0530773 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
774 if (haptic->is_len_gpio_valid == true)
775 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
776
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777 gpio_free(haptic->pdata->hap_en_gpio);
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530778 if (haptic->is_len_gpio_valid == true)
779 gpio_free(haptic->pdata->hap_len_gpio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780
781 /* reset hardware registers */
782 i2c_smbus_write_byte_data(client, ISA1200_HCTRL0,
783 ISA1200_HCTRL0_RESET);
784 i2c_smbus_write_byte_data(client, ISA1200_HCTRL1,
785 ISA1200_HCTRL1_RESET);
786
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700787
Mohan Pallaka65206ee2012-12-05 16:50:48 +0530788 /* destroy mutex */
789 mutex_destroy(&haptic->lock);
790
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700791 /* power-off the chip */
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530792 if (haptic->pdata->regulator_info) {
793 isa1200_reg_power(haptic, false);
794 isa1200_reg_setup(haptic, false);
795 }
796
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797 if (haptic->pdata->power_on)
798 haptic->pdata->power_on(0);
799
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530800 if (haptic->pdata->dev_setup)
801 haptic->pdata->dev_setup(false);
802
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803 kfree(haptic);
804 return 0;
805}
806
807#ifdef CONFIG_PM
808static int isa1200_suspend(struct i2c_client *client, pm_message_t mesg)
809{
810 struct isa1200_chip *haptic = i2c_get_clientdata(client);
811 int ret;
812
813 hrtimer_cancel(&haptic->timer);
814 cancel_work_sync(&haptic->work);
815 /* turn-off current vibration */
816 isa1200_vib_set(haptic, 0);
817
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530818 gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0);
819 if (haptic->is_len_gpio_valid == true)
820 gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0);
821
822 if (haptic->pdata->regulator_info)
823 isa1200_reg_power(haptic, false);
824
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825 if (haptic->pdata->power_on) {
826 ret = haptic->pdata->power_on(0);
827 if (ret) {
828 dev_err(&client->dev, "power-down failed\n");
829 return ret;
830 }
831 }
832
833 return 0;
834}
835
836static int isa1200_resume(struct i2c_client *client)
837{
838 struct isa1200_chip *haptic = i2c_get_clientdata(client);
839 int ret;
840
Mohan Pallaka73787fa2011-09-09 15:14:20 +0530841 if (haptic->pdata->regulator_info)
842 isa1200_reg_power(haptic, true);
843
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844 if (haptic->pdata->power_on) {
845 ret = haptic->pdata->power_on(1);
846 if (ret) {
847 dev_err(&client->dev, "power-up failed\n");
848 return ret;
849 }
850 }
851
852 isa1200_setup(client);
853 return 0;
854}
855#else
856#define isa1200_suspend NULL
857#define isa1200_resume NULL
858#endif
859
860static const struct i2c_device_id isa1200_id[] = {
861 { "isa1200_1", 0 },
862 { },
863};
864MODULE_DEVICE_TABLE(i2c, isa1200_id);
Amy Malochee3f29012012-12-07 18:40:04 -0800865#ifdef CONFIG_OF
866static struct of_device_id isa1200_match_table[] = {
867 { .compatible = "imagis,isa1200",},
868 { },
869};
870#else
871#define isa1200_match_table NULL
872#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700873
874static struct i2c_driver isa1200_driver = {
875 .driver = {
876 .name = "isa1200",
Amy Malochee3f29012012-12-07 18:40:04 -0800877 .of_match_table = isa1200_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878 },
879 .probe = isa1200_probe,
880 .remove = __devexit_p(isa1200_remove),
881 .suspend = isa1200_suspend,
882 .resume = isa1200_resume,
883 .id_table = isa1200_id,
884};
885
886static int __init isa1200_init(void)
887{
888 return i2c_add_driver(&isa1200_driver);
889}
890
891static void __exit isa1200_exit(void)
892{
893 i2c_del_driver(&isa1200_driver);
894}
895
896module_init(isa1200_init);
897module_exit(isa1200_exit);
898
899MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
900MODULE_DESCRIPTION("ISA1200 Haptic Motor driver");
901MODULE_LICENSE("GPL");