16173f8f4SThierry Reding /* 26173f8f4SThierry Reding * Copyright (C) ST-Ericsson SA 2010 36173f8f4SThierry Reding * 46173f8f4SThierry Reding * Author: Arun R Murthy <arun.murthy@stericsson.com> 56173f8f4SThierry Reding * License terms: GNU General Public License (GPL) version 2 66173f8f4SThierry Reding */ 76173f8f4SThierry Reding #include <linux/err.h> 86173f8f4SThierry Reding #include <linux/platform_device.h> 96173f8f4SThierry Reding #include <linux/slab.h> 106173f8f4SThierry Reding #include <linux/pwm.h> 116173f8f4SThierry Reding #include <linux/mfd/abx500.h> 126173f8f4SThierry Reding #include <linux/mfd/abx500/ab8500.h> 136173f8f4SThierry Reding #include <linux/module.h> 146173f8f4SThierry Reding 156173f8f4SThierry Reding /* 166173f8f4SThierry Reding * PWM Out generators 176173f8f4SThierry Reding * Bank: 0x10 186173f8f4SThierry Reding */ 196173f8f4SThierry Reding #define AB8500_PWM_OUT_CTRL1_REG 0x60 206173f8f4SThierry Reding #define AB8500_PWM_OUT_CTRL2_REG 0x61 216173f8f4SThierry Reding #define AB8500_PWM_OUT_CTRL7_REG 0x66 226173f8f4SThierry Reding 236173f8f4SThierry Reding /* backlight driver constants */ 246173f8f4SThierry Reding #define ENABLE_PWM 1 256173f8f4SThierry Reding #define DISABLE_PWM 0 266173f8f4SThierry Reding 276173f8f4SThierry Reding struct ab8500_pwm_chip { 286173f8f4SThierry Reding struct pwm_chip chip; 296173f8f4SThierry Reding }; 306173f8f4SThierry Reding 316173f8f4SThierry Reding static int ab8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 326173f8f4SThierry Reding int duty_ns, int period_ns) 336173f8f4SThierry Reding { 346173f8f4SThierry Reding int ret = 0; 356173f8f4SThierry Reding unsigned int higher_val, lower_val; 366173f8f4SThierry Reding u8 reg; 376173f8f4SThierry Reding 386173f8f4SThierry Reding /* 396173f8f4SThierry Reding * get the first 8 bits that are be written to 406173f8f4SThierry Reding * AB8500_PWM_OUT_CTRL1_REG[0:7] 416173f8f4SThierry Reding */ 426173f8f4SThierry Reding lower_val = duty_ns & 0x00FF; 436173f8f4SThierry Reding /* 446173f8f4SThierry Reding * get bits [9:10] that are to be written to 456173f8f4SThierry Reding * AB8500_PWM_OUT_CTRL2_REG[0:1] 466173f8f4SThierry Reding */ 476173f8f4SThierry Reding higher_val = ((duty_ns & 0x0300) >> 8); 486173f8f4SThierry Reding 496173f8f4SThierry Reding reg = AB8500_PWM_OUT_CTRL1_REG + ((chip->base - 1) * 2); 506173f8f4SThierry Reding 516173f8f4SThierry Reding ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC, 526173f8f4SThierry Reding reg, (u8)lower_val); 536173f8f4SThierry Reding if (ret < 0) 546173f8f4SThierry Reding return ret; 556173f8f4SThierry Reding ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC, 566173f8f4SThierry Reding (reg + 1), (u8)higher_val); 576173f8f4SThierry Reding 586173f8f4SThierry Reding return ret; 596173f8f4SThierry Reding } 606173f8f4SThierry Reding 616173f8f4SThierry Reding static int ab8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) 626173f8f4SThierry Reding { 636173f8f4SThierry Reding int ret; 646173f8f4SThierry Reding 656173f8f4SThierry Reding ret = abx500_mask_and_set_register_interruptible(chip->dev, 666173f8f4SThierry Reding AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG, 676173f8f4SThierry Reding 1 << (chip->base - 1), ENABLE_PWM); 686173f8f4SThierry Reding if (ret < 0) 696173f8f4SThierry Reding dev_err(chip->dev, "%s: Failed to disable PWM, Error %d\n", 706173f8f4SThierry Reding pwm->label, ret); 716173f8f4SThierry Reding return ret; 726173f8f4SThierry Reding } 736173f8f4SThierry Reding 746173f8f4SThierry Reding static void ab8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) 756173f8f4SThierry Reding { 766173f8f4SThierry Reding int ret; 776173f8f4SThierry Reding 786173f8f4SThierry Reding ret = abx500_mask_and_set_register_interruptible(chip->dev, 796173f8f4SThierry Reding AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG, 806173f8f4SThierry Reding 1 << (chip->base - 1), DISABLE_PWM); 816173f8f4SThierry Reding if (ret < 0) 826173f8f4SThierry Reding dev_err(chip->dev, "%s: Failed to disable PWM, Error %d\n", 836173f8f4SThierry Reding pwm->label, ret); 846173f8f4SThierry Reding return; 856173f8f4SThierry Reding } 866173f8f4SThierry Reding 876173f8f4SThierry Reding static const struct pwm_ops ab8500_pwm_ops = { 886173f8f4SThierry Reding .config = ab8500_pwm_config, 896173f8f4SThierry Reding .enable = ab8500_pwm_enable, 906173f8f4SThierry Reding .disable = ab8500_pwm_disable, 916173f8f4SThierry Reding }; 926173f8f4SThierry Reding 933e9fe83dSBill Pemberton static int ab8500_pwm_probe(struct platform_device *pdev) 946173f8f4SThierry Reding { 956173f8f4SThierry Reding struct ab8500_pwm_chip *ab8500; 966173f8f4SThierry Reding int err; 976173f8f4SThierry Reding 986173f8f4SThierry Reding /* 996173f8f4SThierry Reding * Nothing to be done in probe, this is required to get the 1006173f8f4SThierry Reding * device which is required for ab8500 read and write 1016173f8f4SThierry Reding */ 1026173f8f4SThierry Reding ab8500 = kzalloc(sizeof(*ab8500), GFP_KERNEL); 1036173f8f4SThierry Reding if (ab8500 == NULL) { 1046173f8f4SThierry Reding dev_err(&pdev->dev, "failed to allocate memory\n"); 1056173f8f4SThierry Reding return -ENOMEM; 1066173f8f4SThierry Reding } 1076173f8f4SThierry Reding 1086173f8f4SThierry Reding ab8500->chip.dev = &pdev->dev; 1096173f8f4SThierry Reding ab8500->chip.ops = &ab8500_pwm_ops; 1106173f8f4SThierry Reding ab8500->chip.base = pdev->id; 1116173f8f4SThierry Reding ab8500->chip.npwm = 1; 1126173f8f4SThierry Reding 1136173f8f4SThierry Reding err = pwmchip_add(&ab8500->chip); 1146173f8f4SThierry Reding if (err < 0) { 1156173f8f4SThierry Reding kfree(ab8500); 1166173f8f4SThierry Reding return err; 1176173f8f4SThierry Reding } 1186173f8f4SThierry Reding 1196173f8f4SThierry Reding dev_dbg(&pdev->dev, "pwm probe successful\n"); 1206173f8f4SThierry Reding platform_set_drvdata(pdev, ab8500); 1216173f8f4SThierry Reding 1226173f8f4SThierry Reding return 0; 1236173f8f4SThierry Reding } 1246173f8f4SThierry Reding 12577f37917SBill Pemberton static int ab8500_pwm_remove(struct platform_device *pdev) 1266173f8f4SThierry Reding { 1276173f8f4SThierry Reding struct ab8500_pwm_chip *ab8500 = platform_get_drvdata(pdev); 1286173f8f4SThierry Reding int err; 1296173f8f4SThierry Reding 1306173f8f4SThierry Reding err = pwmchip_remove(&ab8500->chip); 1316173f8f4SThierry Reding if (err < 0) 1326173f8f4SThierry Reding return err; 1336173f8f4SThierry Reding 1346173f8f4SThierry Reding dev_dbg(&pdev->dev, "pwm driver removed\n"); 1356173f8f4SThierry Reding kfree(ab8500); 1366173f8f4SThierry Reding 1376173f8f4SThierry Reding return 0; 1386173f8f4SThierry Reding } 1396173f8f4SThierry Reding 1406173f8f4SThierry Reding static struct platform_driver ab8500_pwm_driver = { 1416173f8f4SThierry Reding .driver = { 1426173f8f4SThierry Reding .name = "ab8500-pwm", 1436173f8f4SThierry Reding .owner = THIS_MODULE, 1446173f8f4SThierry Reding }, 1456173f8f4SThierry Reding .probe = ab8500_pwm_probe, 146fd109112SBill Pemberton .remove = ab8500_pwm_remove, 1476173f8f4SThierry Reding }; 1486173f8f4SThierry Reding module_platform_driver(ab8500_pwm_driver); 1496173f8f4SThierry Reding 1506173f8f4SThierry Reding MODULE_AUTHOR("Arun MURTHY <arun.murthy@stericsson.com>"); 1516173f8f4SThierry Reding MODULE_DESCRIPTION("AB8500 Pulse Width Modulation Driver"); 1526173f8f4SThierry Reding MODULE_ALIAS("platform:ab8500-pwm"); 1536173f8f4SThierry Reding MODULE_LICENSE("GPL v2"); 154