1 /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. 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/errno.h> 14 #include <linux/input.h> 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/of.h> 18 #include <linux/of_device.h> 19 #include <linux/platform_device.h> 20 #include <linux/regmap.h> 21 #include <linux/slab.h> 22 23 #define VIB_MAX_LEVEL_mV (3100) 24 #define VIB_MIN_LEVEL_mV (1200) 25 #define VIB_MAX_LEVELS (VIB_MAX_LEVEL_mV - VIB_MIN_LEVEL_mV) 26 27 #define MAX_FF_SPEED 0xff 28 29 struct pm8xxx_regs { 30 unsigned int enable_addr; 31 unsigned int enable_mask; 32 33 unsigned int drv_addr; 34 unsigned int drv_mask; 35 unsigned int drv_shift; 36 unsigned int drv_en_manual_mask; 37 }; 38 39 static const struct pm8xxx_regs pm8058_regs = { 40 .drv_addr = 0x4A, 41 .drv_mask = 0xf8, 42 .drv_shift = 3, 43 .drv_en_manual_mask = 0xfc, 44 }; 45 46 static struct pm8xxx_regs pm8916_regs = { 47 .enable_addr = 0xc046, 48 .enable_mask = BIT(7), 49 .drv_addr = 0xc041, 50 .drv_mask = 0x1F, 51 .drv_shift = 0, 52 .drv_en_manual_mask = 0, 53 }; 54 55 /** 56 * struct pm8xxx_vib - structure to hold vibrator data 57 * @vib_input_dev: input device supporting force feedback 58 * @work: work structure to set the vibration parameters 59 * @regmap: regmap for register read/write 60 * @regs: registers' info 61 * @speed: speed of vibration set from userland 62 * @active: state of vibrator 63 * @level: level of vibration to set in the chip 64 * @reg_vib_drv: regs->drv_addr register value 65 */ 66 struct pm8xxx_vib { 67 struct input_dev *vib_input_dev; 68 struct work_struct work; 69 struct regmap *regmap; 70 const struct pm8xxx_regs *regs; 71 int speed; 72 int level; 73 bool active; 74 u8 reg_vib_drv; 75 }; 76 77 /** 78 * pm8xxx_vib_set - handler to start/stop vibration 79 * @vib: pointer to vibrator structure 80 * @on: state to set 81 */ 82 static int pm8xxx_vib_set(struct pm8xxx_vib *vib, bool on) 83 { 84 int rc; 85 unsigned int val = vib->reg_vib_drv; 86 const struct pm8xxx_regs *regs = vib->regs; 87 88 if (on) 89 val |= (vib->level << regs->drv_shift) & regs->drv_mask; 90 else 91 val &= ~regs->drv_mask; 92 93 rc = regmap_write(vib->regmap, regs->drv_addr, val); 94 if (rc < 0) 95 return rc; 96 97 vib->reg_vib_drv = val; 98 99 if (regs->enable_mask) 100 rc = regmap_update_bits(vib->regmap, regs->enable_addr, 101 on ? regs->enable_mask : 0, val); 102 103 return rc; 104 } 105 106 /** 107 * pm8xxx_work_handler - worker to set vibration level 108 * @work: pointer to work_struct 109 */ 110 static void pm8xxx_work_handler(struct work_struct *work) 111 { 112 struct pm8xxx_vib *vib = container_of(work, struct pm8xxx_vib, work); 113 const struct pm8xxx_regs *regs = vib->regs; 114 int rc; 115 unsigned int val; 116 117 rc = regmap_read(vib->regmap, regs->drv_addr, &val); 118 if (rc < 0) 119 return; 120 121 /* 122 * pmic vibrator supports voltage ranges from 1.2 to 3.1V, so 123 * scale the level to fit into these ranges. 124 */ 125 if (vib->speed) { 126 vib->active = true; 127 vib->level = ((VIB_MAX_LEVELS * vib->speed) / MAX_FF_SPEED) + 128 VIB_MIN_LEVEL_mV; 129 vib->level /= 100; 130 } else { 131 vib->active = false; 132 vib->level = VIB_MIN_LEVEL_mV / 100; 133 } 134 135 pm8xxx_vib_set(vib, vib->active); 136 } 137 138 /** 139 * pm8xxx_vib_close - callback of input close callback 140 * @dev: input device pointer 141 * 142 * Turns off the vibrator. 143 */ 144 static void pm8xxx_vib_close(struct input_dev *dev) 145 { 146 struct pm8xxx_vib *vib = input_get_drvdata(dev); 147 148 cancel_work_sync(&vib->work); 149 if (vib->active) 150 pm8xxx_vib_set(vib, false); 151 } 152 153 /** 154 * pm8xxx_vib_play_effect - function to handle vib effects. 155 * @dev: input device pointer 156 * @data: data of effect 157 * @effect: effect to play 158 * 159 * Currently this driver supports only rumble effects. 160 */ 161 static int pm8xxx_vib_play_effect(struct input_dev *dev, void *data, 162 struct ff_effect *effect) 163 { 164 struct pm8xxx_vib *vib = input_get_drvdata(dev); 165 166 vib->speed = effect->u.rumble.strong_magnitude >> 8; 167 if (!vib->speed) 168 vib->speed = effect->u.rumble.weak_magnitude >> 9; 169 170 schedule_work(&vib->work); 171 172 return 0; 173 } 174 175 static int pm8xxx_vib_probe(struct platform_device *pdev) 176 { 177 struct pm8xxx_vib *vib; 178 struct input_dev *input_dev; 179 int error; 180 unsigned int val; 181 const struct pm8xxx_regs *regs; 182 183 vib = devm_kzalloc(&pdev->dev, sizeof(*vib), GFP_KERNEL); 184 if (!vib) 185 return -ENOMEM; 186 187 vib->regmap = dev_get_regmap(pdev->dev.parent, NULL); 188 if (!vib->regmap) 189 return -ENODEV; 190 191 input_dev = devm_input_allocate_device(&pdev->dev); 192 if (!input_dev) 193 return -ENOMEM; 194 195 INIT_WORK(&vib->work, pm8xxx_work_handler); 196 vib->vib_input_dev = input_dev; 197 198 regs = of_device_get_match_data(&pdev->dev); 199 200 /* operate in manual mode */ 201 error = regmap_read(vib->regmap, regs->drv_addr, &val); 202 if (error < 0) 203 return error; 204 205 val &= regs->drv_en_manual_mask; 206 error = regmap_write(vib->regmap, regs->drv_addr, val); 207 if (error < 0) 208 return error; 209 210 vib->regs = regs; 211 vib->reg_vib_drv = val; 212 213 input_dev->name = "pm8xxx_vib_ffmemless"; 214 input_dev->id.version = 1; 215 input_dev->close = pm8xxx_vib_close; 216 input_set_drvdata(input_dev, vib); 217 input_set_capability(vib->vib_input_dev, EV_FF, FF_RUMBLE); 218 219 error = input_ff_create_memless(input_dev, NULL, 220 pm8xxx_vib_play_effect); 221 if (error) { 222 dev_err(&pdev->dev, 223 "couldn't register vibrator as FF device\n"); 224 return error; 225 } 226 227 error = input_register_device(input_dev); 228 if (error) { 229 dev_err(&pdev->dev, "couldn't register input device\n"); 230 return error; 231 } 232 233 platform_set_drvdata(pdev, vib); 234 return 0; 235 } 236 237 static int __maybe_unused pm8xxx_vib_suspend(struct device *dev) 238 { 239 struct pm8xxx_vib *vib = dev_get_drvdata(dev); 240 241 /* Turn off the vibrator */ 242 pm8xxx_vib_set(vib, false); 243 244 return 0; 245 } 246 247 static SIMPLE_DEV_PM_OPS(pm8xxx_vib_pm_ops, pm8xxx_vib_suspend, NULL); 248 249 static const struct of_device_id pm8xxx_vib_id_table[] = { 250 { .compatible = "qcom,pm8058-vib", .data = &pm8058_regs }, 251 { .compatible = "qcom,pm8921-vib", .data = &pm8058_regs }, 252 { .compatible = "qcom,pm8916-vib", .data = &pm8916_regs }, 253 { } 254 }; 255 MODULE_DEVICE_TABLE(of, pm8xxx_vib_id_table); 256 257 static struct platform_driver pm8xxx_vib_driver = { 258 .probe = pm8xxx_vib_probe, 259 .driver = { 260 .name = "pm8xxx-vib", 261 .pm = &pm8xxx_vib_pm_ops, 262 .of_match_table = pm8xxx_vib_id_table, 263 }, 264 }; 265 module_platform_driver(pm8xxx_vib_driver); 266 267 MODULE_ALIAS("platform:pm8xxx_vib"); 268 MODULE_DESCRIPTION("PMIC8xxx vibrator driver based on ff-memless framework"); 269 MODULE_LICENSE("GPL v2"); 270 MODULE_AUTHOR("Amy Maloche <amaloche@codeaurora.org>"); 271