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> |
| 6 | * Copyright (c) 2010, Code Aurora Forum. All rights reserved. |
| 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> |
| 22 | #include <linux/i2c/isa1200.h> |
| 23 | #include "../staging/android/timed_output.h" |
| 24 | |
| 25 | #define ISA1200_HCTRL0 0x30 |
| 26 | #define ISA1200_HCTRL1 0x31 |
| 27 | #define ISA1200_HCTRL5 0x35 |
| 28 | |
| 29 | #define ISA1200_HCTRL0_RESET 0x01 |
| 30 | #define ISA1200_HCTRL1_RESET 0x4B |
| 31 | |
| 32 | #define ISA1200_HCTRL5_VIB_STRT 0xD5 |
| 33 | #define ISA1200_HCTRL5_VIB_STOP 0x6B |
| 34 | |
| 35 | struct isa1200_chip { |
| 36 | struct i2c_client *client; |
| 37 | struct isa1200_platform_data *pdata; |
| 38 | struct pwm_device *pwm; |
| 39 | struct hrtimer timer; |
| 40 | struct timed_output_dev dev; |
| 41 | struct work_struct work; |
| 42 | spinlock_t lock; |
| 43 | unsigned int enable; |
| 44 | unsigned int period_ns; |
| 45 | }; |
| 46 | |
| 47 | static int isa1200_read_reg(struct i2c_client *client, int reg) |
| 48 | { |
| 49 | int ret; |
| 50 | |
| 51 | ret = i2c_smbus_read_byte_data(client, reg); |
| 52 | if (ret < 0) |
| 53 | dev_err(&client->dev, "%s: err %d\n", __func__, ret); |
| 54 | |
| 55 | return ret; |
| 56 | } |
| 57 | |
| 58 | static int isa1200_write_reg(struct i2c_client *client, int reg, u8 value) |
| 59 | { |
| 60 | int ret; |
| 61 | |
| 62 | ret = i2c_smbus_write_byte_data(client, reg, value); |
| 63 | if (ret < 0) |
| 64 | dev_err(&client->dev, "%s: err %d\n", __func__, ret); |
| 65 | |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | static void isa1200_vib_set(struct isa1200_chip *haptic, int enable) |
| 70 | { |
| 71 | int rc = 0; |
| 72 | |
| 73 | if (enable) { |
| 74 | if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) { |
| 75 | int period_us = haptic->period_ns / 1000; |
| 76 | rc = pwm_config(haptic->pwm, |
| 77 | (period_us * haptic->pdata->duty) / 100, |
| 78 | period_us); |
| 79 | if (rc < 0) |
| 80 | pr_err("%s: pwm_config fail\n", __func__); |
| 81 | rc = pwm_enable(haptic->pwm); |
| 82 | if (rc < 0) |
| 83 | pr_err("%s: pwm_enable fail\n", __func__); |
| 84 | } else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) { |
| 85 | rc = isa1200_write_reg(haptic->client, |
| 86 | ISA1200_HCTRL5, |
| 87 | ISA1200_HCTRL5_VIB_STRT); |
| 88 | if (rc < 0) |
| 89 | pr_err("%s: start vibartion fail\n", __func__); |
| 90 | } |
| 91 | } else { |
| 92 | if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) |
| 93 | pwm_disable(haptic->pwm); |
| 94 | else if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) { |
| 95 | rc = isa1200_write_reg(haptic->client, |
| 96 | ISA1200_HCTRL5, |
| 97 | ISA1200_HCTRL5_VIB_STOP); |
| 98 | if (rc < 0) |
| 99 | pr_err("%s: stop vibartion fail\n", __func__); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static void isa1200_chip_work(struct work_struct *work) |
| 105 | { |
| 106 | struct isa1200_chip *haptic; |
| 107 | |
| 108 | haptic = container_of(work, struct isa1200_chip, work); |
| 109 | isa1200_vib_set(haptic, haptic->enable); |
| 110 | } |
| 111 | |
| 112 | static void isa1200_chip_enable(struct timed_output_dev *dev, int value) |
| 113 | { |
| 114 | struct isa1200_chip *haptic = container_of(dev, struct isa1200_chip, |
| 115 | dev); |
| 116 | unsigned long flags; |
| 117 | |
| 118 | spin_lock_irqsave(&haptic->lock, flags); |
| 119 | hrtimer_cancel(&haptic->timer); |
| 120 | if (value == 0) |
| 121 | haptic->enable = 0; |
| 122 | else { |
| 123 | value = (value > haptic->pdata->max_timeout ? |
| 124 | haptic->pdata->max_timeout : value); |
| 125 | haptic->enable = 1; |
| 126 | hrtimer_start(&haptic->timer, |
| 127 | ktime_set(value / 1000, (value % 1000) * 1000000), |
| 128 | HRTIMER_MODE_REL); |
| 129 | } |
| 130 | spin_unlock_irqrestore(&haptic->lock, flags); |
| 131 | schedule_work(&haptic->work); |
| 132 | } |
| 133 | |
| 134 | static int isa1200_chip_get_time(struct timed_output_dev *dev) |
| 135 | { |
| 136 | struct isa1200_chip *haptic = container_of(dev, struct isa1200_chip, |
| 137 | dev); |
| 138 | |
| 139 | if (hrtimer_active(&haptic->timer)) { |
| 140 | ktime_t r = hrtimer_get_remaining(&haptic->timer); |
| 141 | struct timeval t = ktime_to_timeval(r); |
| 142 | return t.tv_sec * 1000 + t.tv_usec / 1000; |
| 143 | } else |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static enum hrtimer_restart isa1200_vib_timer_func(struct hrtimer *timer) |
| 148 | { |
| 149 | struct isa1200_chip *haptic = container_of(timer, struct isa1200_chip, |
| 150 | timer); |
| 151 | haptic->enable = 0; |
| 152 | schedule_work(&haptic->work); |
| 153 | |
| 154 | return HRTIMER_NORESTART; |
| 155 | } |
| 156 | |
| 157 | static void dump_isa1200_reg(char *str, struct i2c_client *client) |
| 158 | { |
| 159 | pr_debug("%s reg0x%x=0x%x, reg0x%x=0x%x, reg0x%x=0x%x\n", str, |
| 160 | ISA1200_HCTRL0, isa1200_read_reg(client, ISA1200_HCTRL0), |
| 161 | ISA1200_HCTRL1, isa1200_read_reg(client, ISA1200_HCTRL1), |
| 162 | ISA1200_HCTRL5, isa1200_read_reg(client, ISA1200_HCTRL5)); |
| 163 | } |
| 164 | |
| 165 | static int isa1200_setup(struct i2c_client *client) |
| 166 | { |
| 167 | struct isa1200_chip *haptic = i2c_get_clientdata(client); |
| 168 | int value, temp, rc; |
| 169 | |
| 170 | gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 0); |
| 171 | udelay(250); |
| 172 | gpio_set_value_cansleep(haptic->pdata->hap_en_gpio, 1); |
| 173 | |
| 174 | value = (haptic->pdata->smart_en << 3) | |
| 175 | (haptic->pdata->is_erm << 5) | |
| 176 | (haptic->pdata->ext_clk_en << 7); |
| 177 | |
| 178 | rc = isa1200_write_reg(client, ISA1200_HCTRL1, value); |
| 179 | if (rc < 0) { |
| 180 | pr_err("%s: i2c write failure\n", __func__); |
| 181 | return rc; |
| 182 | } |
| 183 | |
| 184 | if (haptic->pdata->mode_ctrl == PWM_GEN_MODE) { |
| 185 | temp = haptic->pdata->pwm_fd.pwm_div; |
| 186 | if (temp < 128 || temp > 1024 || temp % 128) { |
| 187 | pr_err("%s: Invalid divider\n", __func__); |
| 188 | goto reset_hctrl1; |
| 189 | } |
| 190 | value = ((temp >> 7) - 1); |
| 191 | } else if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) { |
| 192 | temp = haptic->pdata->pwm_fd.pwm_freq; |
| 193 | if (temp < 22400 || temp > 172600 || temp % 22400) { |
| 194 | pr_err("%s: Invalid frequency\n", __func__); |
| 195 | goto reset_hctrl1; |
| 196 | } |
| 197 | value = ((temp / 22400) - 1); |
| 198 | haptic->period_ns = NSEC_PER_SEC / temp; |
| 199 | } |
| 200 | |
| 201 | value |= (haptic->pdata->mode_ctrl << 3) | |
| 202 | (haptic->pdata->overdrive_high << 5) | |
| 203 | (haptic->pdata->overdrive_en << 5) | |
| 204 | (haptic->pdata->chip_en << 7); |
| 205 | |
| 206 | rc = isa1200_write_reg(client, ISA1200_HCTRL0, value); |
| 207 | if (rc < 0) { |
| 208 | pr_err("%s: i2c write failure\n", __func__); |
| 209 | goto reset_hctrl1; |
| 210 | } |
| 211 | |
| 212 | dump_isa1200_reg("new:", client); |
| 213 | return 0; |
| 214 | |
| 215 | reset_hctrl1: |
| 216 | i2c_smbus_write_byte_data(client, ISA1200_HCTRL1, |
| 217 | ISA1200_HCTRL1_RESET); |
| 218 | return rc; |
| 219 | } |
| 220 | |
| 221 | static int __devinit isa1200_probe(struct i2c_client *client, |
| 222 | const struct i2c_device_id *id) |
| 223 | { |
| 224 | struct isa1200_chip *haptic; |
| 225 | struct isa1200_platform_data *pdata; |
| 226 | int ret; |
| 227 | |
| 228 | if (!i2c_check_functionality(client->adapter, |
| 229 | I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 230 | dev_err(&client->dev, "%s: no support for i2c read/write" |
| 231 | "byte data\n", __func__); |
| 232 | return -EIO; |
| 233 | } |
| 234 | |
| 235 | pdata = client->dev.platform_data; |
| 236 | if (!pdata) { |
| 237 | dev_err(&client->dev, "%s: no platform data\n", __func__); |
| 238 | return -EINVAL; |
| 239 | } |
| 240 | |
| 241 | if (pdata->dev_setup) { |
| 242 | ret = pdata->dev_setup(true); |
| 243 | if (ret < 0) { |
| 244 | dev_err(&client->dev, "dev setup failed\n"); |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | haptic = kzalloc(sizeof(struct isa1200_chip), GFP_KERNEL); |
| 250 | if (!haptic) { |
| 251 | ret = -ENOMEM; |
| 252 | goto mem_alloc_fail; |
| 253 | } |
| 254 | haptic->client = client; |
| 255 | haptic->enable = 0; |
| 256 | haptic->pdata = pdata; |
| 257 | |
| 258 | if (pdata->power_on) { |
| 259 | ret = pdata->power_on(1); |
| 260 | if (ret) { |
| 261 | dev_err(&client->dev, "%s: power-up failed\n", |
| 262 | __func__); |
| 263 | goto pwr_up_fail; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | spin_lock_init(&haptic->lock); |
| 268 | INIT_WORK(&haptic->work, isa1200_chip_work); |
| 269 | |
| 270 | hrtimer_init(&haptic->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 271 | haptic->timer.function = isa1200_vib_timer_func; |
| 272 | |
| 273 | /*register with timed output class*/ |
| 274 | haptic->dev.name = pdata->name; |
| 275 | haptic->dev.get_time = isa1200_chip_get_time; |
| 276 | haptic->dev.enable = isa1200_chip_enable; |
| 277 | ret = timed_output_dev_register(&haptic->dev); |
| 278 | if (ret < 0) |
| 279 | goto timed_reg_fail; |
| 280 | |
| 281 | i2c_set_clientdata(client, haptic); |
| 282 | |
| 283 | ret = gpio_is_valid(pdata->hap_en_gpio); |
| 284 | if (ret) { |
| 285 | ret = gpio_request(pdata->hap_en_gpio, "haptic_gpio"); |
| 286 | if (ret) { |
| 287 | dev_err(&client->dev, "%s: gpio %d request failed\n", |
| 288 | __func__, pdata->hap_en_gpio); |
| 289 | goto gpio_fail; |
| 290 | } |
| 291 | } else { |
| 292 | dev_err(&client->dev, "%s: Invalid gpio %d\n", __func__, |
| 293 | pdata->hap_en_gpio); |
| 294 | goto gpio_fail; |
| 295 | } |
| 296 | |
| 297 | ret = isa1200_setup(client); |
| 298 | if (ret) { |
| 299 | dev_err(&client->dev, "%s: setup fail %d\n", __func__, ret); |
| 300 | goto gpio_fail; |
| 301 | } |
| 302 | |
| 303 | if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) { |
| 304 | haptic->pwm = pwm_request(pdata->pwm_ch_id, id->name); |
| 305 | if (IS_ERR(haptic->pwm)) { |
| 306 | dev_err(&client->dev, "%s: pwm request failed\n", |
| 307 | __func__); |
| 308 | ret = PTR_ERR(haptic->pwm); |
| 309 | goto reset_hctrl0; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | printk(KERN_INFO "%s: %s registered\n", __func__, id->name); |
| 314 | return 0; |
| 315 | |
| 316 | reset_hctrl0: |
| 317 | i2c_smbus_write_byte_data(client, ISA1200_HCTRL0, |
| 318 | ISA1200_HCTRL0_RESET); |
| 319 | gpio_fail: |
| 320 | timed_output_dev_unregister(&haptic->dev); |
| 321 | timed_reg_fail: |
| 322 | if (pdata->power_on) |
| 323 | pdata->power_on(0); |
| 324 | pwr_up_fail: |
| 325 | kfree(haptic); |
| 326 | mem_alloc_fail: |
| 327 | if (pdata->dev_setup) |
| 328 | pdata->dev_setup(false); |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | static int __devexit isa1200_remove(struct i2c_client *client) |
| 333 | { |
| 334 | struct isa1200_chip *haptic = i2c_get_clientdata(client); |
| 335 | |
| 336 | hrtimer_cancel(&haptic->timer); |
| 337 | cancel_work_sync(&haptic->work); |
| 338 | |
| 339 | /* turn-off current vibration */ |
| 340 | isa1200_vib_set(haptic, 0); |
| 341 | |
| 342 | if (haptic->pdata->mode_ctrl == PWM_INPUT_MODE) |
| 343 | pwm_free(haptic->pwm); |
| 344 | |
| 345 | timed_output_dev_unregister(&haptic->dev); |
| 346 | gpio_free(haptic->pdata->hap_en_gpio); |
| 347 | |
| 348 | /* reset hardware registers */ |
| 349 | i2c_smbus_write_byte_data(client, ISA1200_HCTRL0, |
| 350 | ISA1200_HCTRL0_RESET); |
| 351 | i2c_smbus_write_byte_data(client, ISA1200_HCTRL1, |
| 352 | ISA1200_HCTRL1_RESET); |
| 353 | |
| 354 | if (haptic->pdata->dev_setup) |
| 355 | haptic->pdata->dev_setup(false); |
| 356 | |
| 357 | /* power-off the chip */ |
| 358 | if (haptic->pdata->power_on) |
| 359 | haptic->pdata->power_on(0); |
| 360 | |
| 361 | kfree(haptic); |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | #ifdef CONFIG_PM |
| 366 | static int isa1200_suspend(struct i2c_client *client, pm_message_t mesg) |
| 367 | { |
| 368 | struct isa1200_chip *haptic = i2c_get_clientdata(client); |
| 369 | int ret; |
| 370 | |
| 371 | hrtimer_cancel(&haptic->timer); |
| 372 | cancel_work_sync(&haptic->work); |
| 373 | /* turn-off current vibration */ |
| 374 | isa1200_vib_set(haptic, 0); |
| 375 | |
| 376 | if (haptic->pdata->power_on) { |
| 377 | ret = haptic->pdata->power_on(0); |
| 378 | if (ret) { |
| 379 | dev_err(&client->dev, "power-down failed\n"); |
| 380 | return ret; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int isa1200_resume(struct i2c_client *client) |
| 388 | { |
| 389 | struct isa1200_chip *haptic = i2c_get_clientdata(client); |
| 390 | int ret; |
| 391 | |
| 392 | if (haptic->pdata->power_on) { |
| 393 | ret = haptic->pdata->power_on(1); |
| 394 | if (ret) { |
| 395 | dev_err(&client->dev, "power-up failed\n"); |
| 396 | return ret; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | isa1200_setup(client); |
| 401 | return 0; |
| 402 | } |
| 403 | #else |
| 404 | #define isa1200_suspend NULL |
| 405 | #define isa1200_resume NULL |
| 406 | #endif |
| 407 | |
| 408 | static const struct i2c_device_id isa1200_id[] = { |
| 409 | { "isa1200_1", 0 }, |
| 410 | { }, |
| 411 | }; |
| 412 | MODULE_DEVICE_TABLE(i2c, isa1200_id); |
| 413 | |
| 414 | static struct i2c_driver isa1200_driver = { |
| 415 | .driver = { |
| 416 | .name = "isa1200", |
| 417 | }, |
| 418 | .probe = isa1200_probe, |
| 419 | .remove = __devexit_p(isa1200_remove), |
| 420 | .suspend = isa1200_suspend, |
| 421 | .resume = isa1200_resume, |
| 422 | .id_table = isa1200_id, |
| 423 | }; |
| 424 | |
| 425 | static int __init isa1200_init(void) |
| 426 | { |
| 427 | return i2c_add_driver(&isa1200_driver); |
| 428 | } |
| 429 | |
| 430 | static void __exit isa1200_exit(void) |
| 431 | { |
| 432 | i2c_del_driver(&isa1200_driver); |
| 433 | } |
| 434 | |
| 435 | module_init(isa1200_init); |
| 436 | module_exit(isa1200_exit); |
| 437 | |
| 438 | MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>"); |
| 439 | MODULE_DESCRIPTION("ISA1200 Haptic Motor driver"); |
| 440 | MODULE_LICENSE("GPL"); |