Vikram Mulukutla | f98205f | 2011-02-19 17:08:15 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/pmic8058-vibrator.h> |
| 20 | #include <linux/mfd/pmic8058.h> |
| 21 | #include <linux/pm.h> |
| 22 | #include <linux/pm_runtime.h> |
| 23 | #include <linux/slab.h> |
| 24 | |
| 25 | #include "../staging/android/timed_output.h" |
| 26 | |
| 27 | #define VIB_DRV 0x4A |
| 28 | |
| 29 | #define VIB_DRV_SEL_MASK 0xf8 |
| 30 | #define VIB_DRV_SEL_SHIFT 0x03 |
| 31 | #define VIB_DRV_EN_MANUAL_MASK 0xfc |
| 32 | |
| 33 | #define VIB_MAX_LEVEL_mV 3100 |
| 34 | #define VIB_MIN_LEVEL_mV 1200 |
| 35 | |
| 36 | struct pmic8058_vib { |
| 37 | struct hrtimer vib_timer; |
| 38 | struct timed_output_dev timed_dev; |
| 39 | spinlock_t lock; |
| 40 | struct work_struct work; |
| 41 | |
| 42 | struct device *dev; |
| 43 | struct pmic8058_vibrator_pdata *pdata; |
| 44 | int state; |
| 45 | int level; |
| 46 | u8 reg_vib_drv; |
| 47 | |
| 48 | struct pm8058_chip *pm_chip; |
| 49 | }; |
| 50 | |
| 51 | /* REVISIT: just for debugging, will be removed in final working version */ |
| 52 | static void __dump_vib_regs(struct pmic8058_vib *vib, char *msg) |
| 53 | { |
| 54 | u8 temp; |
| 55 | |
| 56 | dev_dbg(vib->dev, "%s\n", msg); |
| 57 | |
| 58 | pm8058_read(vib->pm_chip, VIB_DRV, &temp, 1); |
| 59 | dev_dbg(vib->dev, "VIB_DRV - %X\n", temp); |
| 60 | } |
| 61 | |
| 62 | static int pmic8058_vib_read_u8(struct pmic8058_vib *vib, |
| 63 | u8 *data, u16 reg) |
| 64 | { |
| 65 | int rc; |
| 66 | |
| 67 | rc = pm8058_read(vib->pm_chip, reg, data, 1); |
| 68 | if (rc < 0) |
| 69 | dev_warn(vib->dev, "Error reading pmic8058: %X - ret %X\n", |
| 70 | reg, rc); |
| 71 | |
| 72 | return rc; |
| 73 | } |
| 74 | |
| 75 | static int pmic8058_vib_write_u8(struct pmic8058_vib *vib, |
| 76 | u8 data, u16 reg) |
| 77 | { |
| 78 | int rc; |
| 79 | |
| 80 | rc = pm8058_write(vib->pm_chip, reg, &data, 1); |
| 81 | if (rc < 0) |
| 82 | dev_warn(vib->dev, "Error writing pmic8058: %X - ret %X\n", |
| 83 | reg, rc); |
| 84 | return rc; |
| 85 | } |
| 86 | |
| 87 | static int pmic8058_vib_set(struct pmic8058_vib *vib, int on) |
| 88 | { |
| 89 | int rc; |
| 90 | u8 val; |
| 91 | |
| 92 | if (on) { |
| 93 | rc = pm_runtime_resume(vib->dev); |
| 94 | if (rc < 0) |
| 95 | dev_dbg(vib->dev, "pm_runtime_resume failed\n"); |
| 96 | |
| 97 | val = vib->reg_vib_drv; |
| 98 | val |= ((vib->level << VIB_DRV_SEL_SHIFT) & VIB_DRV_SEL_MASK); |
| 99 | rc = pmic8058_vib_write_u8(vib, val, VIB_DRV); |
| 100 | if (rc < 0) |
| 101 | return rc; |
| 102 | vib->reg_vib_drv = val; |
| 103 | } else { |
| 104 | val = vib->reg_vib_drv; |
| 105 | val &= ~VIB_DRV_SEL_MASK; |
| 106 | rc = pmic8058_vib_write_u8(vib, val, VIB_DRV); |
| 107 | if (rc < 0) |
| 108 | return rc; |
| 109 | vib->reg_vib_drv = val; |
| 110 | |
| 111 | rc = pm_runtime_suspend(vib->dev); |
| 112 | if (rc < 0) |
| 113 | dev_dbg(vib->dev, "pm_runtime_suspend failed\n"); |
| 114 | } |
| 115 | __dump_vib_regs(vib, "vib_set_end"); |
| 116 | |
| 117 | return rc; |
| 118 | } |
| 119 | |
| 120 | static void pmic8058_vib_enable(struct timed_output_dev *dev, int value) |
| 121 | { |
| 122 | struct pmic8058_vib *vib = container_of(dev, struct pmic8058_vib, |
| 123 | timed_dev); |
| 124 | unsigned long flags; |
| 125 | |
Vikram Mulukutla | f98205f | 2011-02-19 17:08:15 -0800 | [diff] [blame] | 126 | retry: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 127 | spin_lock_irqsave(&vib->lock, flags); |
Vikram Mulukutla | f98205f | 2011-02-19 17:08:15 -0800 | [diff] [blame] | 128 | if (hrtimer_try_to_cancel(&vib->vib_timer) < 0) { |
| 129 | spin_unlock_irqrestore(&vib->lock, flags); |
| 130 | cpu_relax(); |
| 131 | goto retry; |
| 132 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 133 | |
| 134 | if (value == 0) |
| 135 | vib->state = 0; |
| 136 | else { |
| 137 | value = (value > vib->pdata->max_timeout_ms ? |
| 138 | vib->pdata->max_timeout_ms : value); |
| 139 | vib->state = 1; |
| 140 | hrtimer_start(&vib->vib_timer, |
| 141 | ktime_set(value / 1000, (value % 1000) * 1000000), |
| 142 | HRTIMER_MODE_REL); |
| 143 | } |
| 144 | spin_unlock_irqrestore(&vib->lock, flags); |
| 145 | schedule_work(&vib->work); |
| 146 | } |
| 147 | |
| 148 | static void pmic8058_vib_update(struct work_struct *work) |
| 149 | { |
| 150 | struct pmic8058_vib *vib = container_of(work, struct pmic8058_vib, |
| 151 | work); |
| 152 | |
| 153 | pmic8058_vib_set(vib, vib->state); |
| 154 | } |
| 155 | |
| 156 | static int pmic8058_vib_get_time(struct timed_output_dev *dev) |
| 157 | { |
| 158 | struct pmic8058_vib *vib = container_of(dev, struct pmic8058_vib, |
| 159 | timed_dev); |
| 160 | |
| 161 | if (hrtimer_active(&vib->vib_timer)) { |
| 162 | ktime_t r = hrtimer_get_remaining(&vib->vib_timer); |
| 163 | return (int) ktime_to_us(r); |
| 164 | } else |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static enum hrtimer_restart pmic8058_vib_timer_func(struct hrtimer *timer) |
| 169 | { |
| 170 | struct pmic8058_vib *vib = container_of(timer, struct pmic8058_vib, |
| 171 | vib_timer); |
| 172 | vib->state = 0; |
| 173 | schedule_work(&vib->work); |
| 174 | return HRTIMER_NORESTART; |
| 175 | } |
| 176 | |
| 177 | #ifdef CONFIG_PM |
| 178 | static int pmic8058_vib_suspend(struct device *dev) |
| 179 | { |
| 180 | struct pmic8058_vib *vib = dev_get_drvdata(dev); |
| 181 | |
| 182 | hrtimer_cancel(&vib->vib_timer); |
| 183 | cancel_work_sync(&vib->work); |
| 184 | /* turn-off vibrator */ |
| 185 | pmic8058_vib_set(vib, 0); |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static struct dev_pm_ops pmic8058_vib_pm_ops = { |
| 190 | .suspend = pmic8058_vib_suspend, |
| 191 | }; |
| 192 | #endif |
| 193 | |
| 194 | static int __devinit pmic8058_vib_probe(struct platform_device *pdev) |
| 195 | |
| 196 | { |
| 197 | struct pmic8058_vibrator_pdata *pdata = pdev->dev.platform_data; |
| 198 | struct pmic8058_vib *vib; |
| 199 | u8 val; |
| 200 | int rc; |
| 201 | |
| 202 | struct pm8058_chip *pm_chip; |
| 203 | |
| 204 | pm_chip = dev_get_drvdata(pdev->dev.parent); |
| 205 | if (pm_chip == NULL) { |
| 206 | dev_err(&pdev->dev, "no parent data passed in\n"); |
| 207 | return -EFAULT; |
| 208 | } |
| 209 | |
| 210 | if (!pdata) |
| 211 | return -EINVAL; |
| 212 | |
| 213 | if (pdata->level_mV < VIB_MIN_LEVEL_mV || |
| 214 | pdata->level_mV > VIB_MAX_LEVEL_mV) |
| 215 | return -EINVAL; |
| 216 | |
| 217 | vib = kzalloc(sizeof(*vib), GFP_KERNEL); |
| 218 | if (!vib) |
| 219 | return -ENOMEM; |
| 220 | |
| 221 | /* Enable runtime PM ops, start in ACTIVE mode */ |
| 222 | rc = pm_runtime_set_active(&pdev->dev); |
| 223 | if (rc < 0) |
| 224 | dev_dbg(&pdev->dev, "unable to set runtime pm state\n"); |
| 225 | pm_runtime_enable(&pdev->dev); |
| 226 | |
| 227 | vib->pm_chip = pm_chip; |
| 228 | vib->pdata = pdata; |
| 229 | vib->level = pdata->level_mV / 100; |
| 230 | vib->dev = &pdev->dev; |
| 231 | |
| 232 | spin_lock_init(&vib->lock); |
| 233 | INIT_WORK(&vib->work, pmic8058_vib_update); |
| 234 | |
| 235 | hrtimer_init(&vib->vib_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 236 | vib->vib_timer.function = pmic8058_vib_timer_func; |
| 237 | |
| 238 | vib->timed_dev.name = "vibrator"; |
| 239 | vib->timed_dev.get_time = pmic8058_vib_get_time; |
| 240 | vib->timed_dev.enable = pmic8058_vib_enable; |
| 241 | |
| 242 | __dump_vib_regs(vib, "boot_vib_default"); |
| 243 | |
| 244 | /* operate in manual mode */ |
| 245 | rc = pmic8058_vib_read_u8(vib, &val, VIB_DRV); |
| 246 | if (rc < 0) |
| 247 | goto err_read_vib; |
| 248 | val &= ~VIB_DRV_EN_MANUAL_MASK; |
| 249 | rc = pmic8058_vib_write_u8(vib, val, VIB_DRV); |
| 250 | if (rc < 0) |
| 251 | goto err_read_vib; |
| 252 | |
| 253 | vib->reg_vib_drv = val; |
| 254 | |
| 255 | rc = timed_output_dev_register(&vib->timed_dev); |
| 256 | if (rc < 0) |
| 257 | goto err_read_vib; |
| 258 | |
| 259 | pmic8058_vib_enable(&vib->timed_dev, pdata->initial_vibrate_ms); |
| 260 | |
| 261 | platform_set_drvdata(pdev, vib); |
| 262 | |
| 263 | pm_runtime_set_suspended(&pdev->dev); |
| 264 | return 0; |
| 265 | |
| 266 | err_read_vib: |
| 267 | pm_runtime_set_suspended(&pdev->dev); |
| 268 | pm_runtime_disable(&pdev->dev); |
| 269 | kfree(vib); |
| 270 | return rc; |
| 271 | } |
| 272 | |
| 273 | static int __devexit pmic8058_vib_remove(struct platform_device *pdev) |
| 274 | { |
| 275 | struct pmic8058_vib *vib = platform_get_drvdata(pdev); |
| 276 | |
| 277 | pm_runtime_disable(&pdev->dev); |
| 278 | cancel_work_sync(&vib->work); |
| 279 | hrtimer_cancel(&vib->vib_timer); |
| 280 | timed_output_dev_unregister(&vib->timed_dev); |
| 281 | kfree(vib); |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static struct platform_driver pmic8058_vib_driver = { |
| 287 | .probe = pmic8058_vib_probe, |
| 288 | .remove = __devexit_p(pmic8058_vib_remove), |
| 289 | .driver = { |
| 290 | .name = "pm8058-vib", |
| 291 | .owner = THIS_MODULE, |
| 292 | #ifdef CONFIG_PM |
| 293 | .pm = &pmic8058_vib_pm_ops, |
| 294 | #endif |
| 295 | }, |
| 296 | }; |
| 297 | |
| 298 | static int __init pmic8058_vib_init(void) |
| 299 | { |
| 300 | return platform_driver_register(&pmic8058_vib_driver); |
| 301 | } |
| 302 | module_init(pmic8058_vib_init); |
| 303 | |
| 304 | static void __exit pmic8058_vib_exit(void) |
| 305 | { |
| 306 | platform_driver_unregister(&pmic8058_vib_driver); |
| 307 | } |
| 308 | module_exit(pmic8058_vib_exit); |
| 309 | |
| 310 | MODULE_ALIAS("platform:pmic8058_vib"); |
| 311 | MODULE_DESCRIPTION("PMIC8058 vibrator driver"); |
| 312 | MODULE_LICENSE("GPL v2"); |