Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * isa1200.c - Haptic Motor |
| 3 | * |
| 4 | * Copyright (C) 2009 Samsung Electronics |
| 5 | * Kyungmin Park <kyungmin.park@samsung.com> |
Amy Maloche | 6215823 | 2012-12-07 18:40:04 -0800 | [diff] [blame] | 6 | * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 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 Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 22 | #include <linux/regulator/consumer.h> |
Mohan Pallaka | 32f20a7 | 2012-06-14 14:41:11 +0530 | [diff] [blame] | 23 | #include <linux/clk.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 24 | #include <linux/i2c/isa1200.h> |
| 25 | #include "../staging/android/timed_output.h" |
Amy Maloche | e3f2901 | 2012-12-07 18:40:04 -0800 | [diff] [blame] | 26 | #include <linux/of_gpio.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 27 | |
| 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 Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 37 | #define ISA1200_POWER_DOWN_MASK 0x7F |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 38 | |
| 39 | struct 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 Pallaka | 65206ee | 2012-12-05 16:50:48 +0530 | [diff] [blame] | 46 | struct mutex lock; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 47 | unsigned int enable; |
| 48 | unsigned int period_ns; |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 49 | bool is_len_gpio_valid; |
| 50 | struct regulator **regs; |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 51 | bool clk_on; |
| 52 | u8 hctrl0_val; |
Mohan Pallaka | 32f20a7 | 2012-06-14 14:41:11 +0530 | [diff] [blame] | 53 | struct clk *pwm_clk; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static 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 | |
| 67 | static 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 | |
| 78 | static void isa1200_vib_set(struct isa1200_chip *haptic, int enable) |
| 79 | { |
| 80 | int rc = 0; |
| 81 | |
| 82 | if (enable) { |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 83 | /* 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 96 | if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) { |
| 97 | int period_us = haptic->period_ns / 1000; |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 98 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 99 | rc = pwm_config(haptic->pwm, |
| 100 | (period_us * haptic->pdata->duty) / 100, |
| 101 | period_us); |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 102 | if (rc < 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 103 | pr_err("%s: pwm_config fail\n", __func__); |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 104 | goto chip_dwn; |
| 105 | } |
| 106 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 107 | rc = pwm_enable(haptic->pwm); |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 108 | if (rc < 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 109 | pr_err("%s: pwm_enable fail\n", __func__); |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 110 | goto chip_dwn; |
| 111 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 112 | } else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) { |
Mohan Pallaka | 32f20a7 | 2012-06-14 14:41:11 +0530 | [diff] [blame] | 113 | /* check for board specific clk callback */ |
| 114 | if (haptic->pdata->clk_enable) { |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 115 | rc = haptic->pdata->clk_enable(true); |
| 116 | if (rc < 0) { |
Mohan Pallaka | 32f20a7 | 2012-06-14 14:41:11 +0530 | [diff] [blame] | 117 | 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 Maloche | e3f2901 | 2012-12-07 18:40:04 -0800 | [diff] [blame] | 125 | rc = clk_prepare_enable(haptic->pwm_clk); |
Mohan Pallaka | 32f20a7 | 2012-06-14 14:41:11 +0530 | [diff] [blame] | 126 | if (rc < 0) { |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 127 | pr_err("%s: clk enable failed\n", |
| 128 | __func__); |
Mohan Pallaka | 32f20a7 | 2012-06-14 14:41:11 +0530 | [diff] [blame] | 129 | goto dis_clk_cb; |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 130 | } |
| 131 | haptic->clk_on = true; |
| 132 | } |
| 133 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 134 | rc = isa1200_write_reg(haptic->client, |
| 135 | ISA1200_HCTRL5, |
| 136 | ISA1200_HCTRL5_VIB_STRT); |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 137 | if (rc < 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 138 | pr_err("%s: start vibartion fail\n", __func__); |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 139 | goto dis_clk; |
| 140 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 141 | } |
| 142 | } else { |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 143 | /* 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 157 | pwm_disable(haptic->pwm); |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 158 | } else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 159 | 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 Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 164 | |
| 165 | /* de-vote clock */ |
Mohan Pallaka | 32f20a7 | 2012-06-14 14:41:11 +0530 | [diff] [blame] | 166 | if (haptic->pdata->need_pwm_clk && haptic->clk_on) { |
Amy Maloche | 6215823 | 2012-12-07 18:40:04 -0800 | [diff] [blame] | 167 | clk_disable_unprepare(haptic->pwm_clk); |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 168 | haptic->clk_on = false; |
| 169 | } |
Mohan Pallaka | 32f20a7 | 2012-06-14 14:41:11 +0530 | [diff] [blame] | 170 | /* 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 Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | return; |
| 181 | |
| 182 | dis_clk: |
Mohan Pallaka | 32f20a7 | 2012-06-14 14:41:11 +0530 | [diff] [blame] | 183 | if (haptic->pdata->need_pwm_clk && haptic->clk_on) { |
Amy Maloche | 6215823 | 2012-12-07 18:40:04 -0800 | [diff] [blame] | 184 | clk_disable_unprepare(haptic->pwm_clk); |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 185 | haptic->clk_on = false; |
| 186 | } |
Mohan Pallaka | 32f20a7 | 2012-06-14 14:41:11 +0530 | [diff] [blame] | 187 | |
| 188 | dis_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 Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 195 | chip_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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static 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 | |
| 216 | static 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 220 | |
Mohan Pallaka | 65206ee | 2012-12-05 16:50:48 +0530 | [diff] [blame] | 221 | mutex_lock(&haptic->lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 222 | 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 Pallaka | 65206ee | 2012-12-05 16:50:48 +0530 | [diff] [blame] | 233 | mutex_unlock(&haptic->lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 234 | schedule_work(&haptic->work); |
| 235 | } |
| 236 | |
| 237 | static 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 | |
| 250 | static 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 | |
| 260 | static 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 | |
| 268 | static int isa1200_setup(struct i2c_client *client) |
| 269 | { |
| 270 | struct isa1200_chip *haptic = i2c_get_clientdata(client); |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 271 | int temp, rc; |
| 272 | u8 value; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 273 | |
| 274 | gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0); |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 275 | if (haptic->is_len_gpio_valid == true) |
| 276 | gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 0); |
| 277 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 278 | udelay(250); |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 279 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 280 | gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 1); |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 281 | if (haptic->is_len_gpio_valid == true) |
| 282 | gpio_set_value_cansleep(haptic->pdata->hap_len_gpio, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 283 | |
| 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 Pallaka | acdca0f | 2011-11-21 13:08:00 +0530 | [diff] [blame] | 291 | goto reset_gpios; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 292 | } |
| 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 Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 322 | /* 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 336 | dump_isa1200_reg("new:", client); |
| 337 | return 0; |
| 338 | |
| 339 | reset_hctrl1: |
| 340 | i2c_smbus_write_byte_data(client, ISA1200_HCTRL1, |
| 341 | ISA1200_HCTRL1_RESET); |
Mohan Pallaka | acdca0f | 2011-11-21 13:08:00 +0530 | [diff] [blame] | 342 | reset_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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 346 | return rc; |
| 347 | } |
| 348 | |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 349 | static 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 | |
| 378 | regs_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 | |
| 388 | static 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 Maloche | e3f2901 | 2012-12-07 18:40:04 -0800 | [diff] [blame] | 410 | reg_info[i].name); |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 411 | if (IS_ERR(haptic->regs[i])) { |
| 412 | rc = PTR_ERR(haptic->regs[i]); |
Amy Maloche | e3f2901 | 2012-12-07 18:40:04 -0800 | [diff] [blame] | 413 | pr_err("%s:regulator get failed(%d)\n", __func__, rc); |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 414 | 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 | |
| 431 | put_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 Maloche | e3f2901 | 2012-12-07 18:40:04 -0800 | [diff] [blame] | 442 | #ifdef CONFIG_OF |
| 443 | static 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 |
| 566 | static int isa1200_parse_dt(struct device *dev, |
| 567 | struct isa1200_platform_data *pdata) |
| 568 | { |
| 569 | return -ENODEV; |
| 570 | } |
| 571 | #endif |
| 572 | |
| 573 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 574 | static 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 Maloche | e3f2901 | 2012-12-07 18:40:04 -0800 | [diff] [blame] | 588 | 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 604 | 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 Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 626 | 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 642 | 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 Pallaka | 65206ee | 2012-12-05 16:50:48 +0530 | [diff] [blame] | 651 | mutex_init(&haptic->lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 652 | INIT_WORK(&haptic->work, isa1200_chip_work); |
Mohan Pallaka | ace9fa9 | 2012-05-11 13:05:40 +0530 | [diff] [blame] | 653 | haptic->clk_on = false; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 654 | |
| 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 Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 670 | ret = gpio_request(pdata->hap_en_gpio, "haptic_en_gpio"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 671 | if (ret) { |
| 672 | dev_err(&client->dev, "%s: gpio %d request failed\n", |
| 673 | __func__, pdata->hap_en_gpio); |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 674 | goto hen_gpio_fail; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 675 | } |
| 676 | } else { |
| 677 | dev_err(&client->dev, "%s: Invalid gpio %d\n", __func__, |
| 678 | pdata->hap_en_gpio); |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 679 | 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | ret = isa1200_setup(client); |
| 700 | if (ret) { |
| 701 | dev_err(&client->dev, "%s: setup fail %d\n", __func__, ret); |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 702 | goto setup_fail; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 703 | } |
| 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 Pallaka | 32f20a7 | 2012-06-14 14:41:11 +0530 | [diff] [blame] | 713 | } 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | printk(KERN_INFO "%s: %s registered\n", __func__, id->name); |
| 723 | return 0; |
| 724 | |
| 725 | reset_hctrl0: |
Mohan Pallaka | acdca0f | 2011-11-21 13:08:00 +0530 | [diff] [blame] | 726 | 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 731 | i2c_smbus_write_byte_data(client, ISA1200_HCTRL0, |
| 732 | ISA1200_HCTRL0_RESET); |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 733 | setup_fail: |
| 734 | if (haptic->is_len_gpio_valid == true) |
| 735 | gpio_free(pdata->hap_len_gpio); |
| 736 | len_gpio_fail: |
| 737 | gpio_free(pdata->hap_en_gpio); |
| 738 | hen_gpio_fail: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 739 | timed_output_dev_unregister(&haptic->dev); |
| 740 | timed_reg_fail: |
Mohan Pallaka | 65206ee | 2012-12-05 16:50:48 +0530 | [diff] [blame] | 741 | mutex_destroy(&haptic->lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 742 | if (pdata->power_on) |
| 743 | pdata->power_on(0); |
| 744 | pwr_up_fail: |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 745 | if (pdata->regulator_info) |
| 746 | isa1200_reg_power(haptic, false); |
| 747 | reg_pwr_fail: |
| 748 | if (pdata->regulator_info) |
| 749 | isa1200_reg_setup(haptic, false); |
| 750 | reg_setup_fail: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 751 | kfree(haptic); |
| 752 | mem_alloc_fail: |
| 753 | if (pdata->dev_setup) |
| 754 | pdata->dev_setup(false); |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | static 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 Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 772 | |
Mohan Pallaka | acdca0f | 2011-11-21 13:08:00 +0530 | [diff] [blame] | 773 | 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 777 | gpio_free(haptic->pdata->hap_en_gpio); |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 778 | if (haptic->is_len_gpio_valid == true) |
| 779 | gpio_free(haptic->pdata->hap_len_gpio); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 780 | |
| 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 787 | |
Mohan Pallaka | 65206ee | 2012-12-05 16:50:48 +0530 | [diff] [blame] | 788 | /* destroy mutex */ |
| 789 | mutex_destroy(&haptic->lock); |
| 790 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 791 | /* power-off the chip */ |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 792 | if (haptic->pdata->regulator_info) { |
| 793 | isa1200_reg_power(haptic, false); |
| 794 | isa1200_reg_setup(haptic, false); |
| 795 | } |
| 796 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 797 | if (haptic->pdata->power_on) |
| 798 | haptic->pdata->power_on(0); |
| 799 | |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 800 | if (haptic->pdata->dev_setup) |
| 801 | haptic->pdata->dev_setup(false); |
| 802 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 803 | kfree(haptic); |
| 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | #ifdef CONFIG_PM |
| 808 | static 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 Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 818 | 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 825 | 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 | |
| 836 | static int isa1200_resume(struct i2c_client *client) |
| 837 | { |
| 838 | struct isa1200_chip *haptic = i2c_get_clientdata(client); |
| 839 | int ret; |
| 840 | |
Mohan Pallaka | 73787fa | 2011-09-09 15:14:20 +0530 | [diff] [blame] | 841 | if (haptic->pdata->regulator_info) |
| 842 | isa1200_reg_power(haptic, true); |
| 843 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 844 | 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 | |
| 860 | static const struct i2c_device_id isa1200_id[] = { |
| 861 | { "isa1200_1", 0 }, |
| 862 | { }, |
| 863 | }; |
| 864 | MODULE_DEVICE_TABLE(i2c, isa1200_id); |
Amy Maloche | e3f2901 | 2012-12-07 18:40:04 -0800 | [diff] [blame] | 865 | #ifdef CONFIG_OF |
| 866 | static struct of_device_id isa1200_match_table[] = { |
| 867 | { .compatible = "imagis,isa1200",}, |
| 868 | { }, |
| 869 | }; |
| 870 | #else |
| 871 | #define isa1200_match_table NULL |
| 872 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 873 | |
| 874 | static struct i2c_driver isa1200_driver = { |
| 875 | .driver = { |
| 876 | .name = "isa1200", |
Amy Maloche | e3f2901 | 2012-12-07 18:40:04 -0800 | [diff] [blame] | 877 | .of_match_table = isa1200_match_table, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 878 | }, |
| 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 | |
| 886 | static int __init isa1200_init(void) |
| 887 | { |
| 888 | return i2c_add_driver(&isa1200_driver); |
| 889 | } |
| 890 | |
| 891 | static void __exit isa1200_exit(void) |
| 892 | { |
| 893 | i2c_del_driver(&isa1200_driver); |
| 894 | } |
| 895 | |
| 896 | module_init(isa1200_init); |
| 897 | module_exit(isa1200_exit); |
| 898 | |
| 899 | MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>"); |
| 900 | MODULE_DESCRIPTION("ISA1200 Haptic Motor driver"); |
| 901 | MODULE_LICENSE("GPL"); |