1af873fceSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 26173f8f4SThierry Reding /* 36173f8f4SThierry Reding * Copyright (C) ST-Ericsson SA 2010 46173f8f4SThierry Reding * 56173f8f4SThierry Reding * Author: Arun R Murthy <arun.murthy@stericsson.com> 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 struct ab8500_pwm_chip { 246173f8f4SThierry Reding struct pwm_chip chip; 256173f8f4SThierry Reding }; 266173f8f4SThierry Reding 276173f8f4SThierry Reding static int ab8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 286173f8f4SThierry Reding int duty_ns, int period_ns) 296173f8f4SThierry Reding { 306173f8f4SThierry Reding int ret = 0; 316173f8f4SThierry Reding unsigned int higher_val, lower_val; 326173f8f4SThierry Reding u8 reg; 336173f8f4SThierry Reding 346173f8f4SThierry Reding /* 356173f8f4SThierry Reding * get the first 8 bits that are be written to 366173f8f4SThierry Reding * AB8500_PWM_OUT_CTRL1_REG[0:7] 376173f8f4SThierry Reding */ 386173f8f4SThierry Reding lower_val = duty_ns & 0x00FF; 396173f8f4SThierry Reding /* 406173f8f4SThierry Reding * get bits [9:10] that are to be written to 416173f8f4SThierry Reding * AB8500_PWM_OUT_CTRL2_REG[0:1] 426173f8f4SThierry Reding */ 436173f8f4SThierry Reding higher_val = ((duty_ns & 0x0300) >> 8); 446173f8f4SThierry Reding 456173f8f4SThierry Reding reg = AB8500_PWM_OUT_CTRL1_REG + ((chip->base - 1) * 2); 466173f8f4SThierry Reding 476173f8f4SThierry Reding ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC, 486173f8f4SThierry Reding reg, (u8)lower_val); 496173f8f4SThierry Reding if (ret < 0) 506173f8f4SThierry Reding return ret; 516173f8f4SThierry Reding ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC, 526173f8f4SThierry Reding (reg + 1), (u8)higher_val); 536173f8f4SThierry Reding 546173f8f4SThierry Reding return ret; 556173f8f4SThierry Reding } 566173f8f4SThierry Reding 576173f8f4SThierry Reding static int ab8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) 586173f8f4SThierry Reding { 596173f8f4SThierry Reding int ret; 606173f8f4SThierry Reding 616173f8f4SThierry Reding ret = abx500_mask_and_set_register_interruptible(chip->dev, 626173f8f4SThierry Reding AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG, 6354b02347SAxel Lin 1 << (chip->base - 1), 1 << (chip->base - 1)); 646173f8f4SThierry Reding if (ret < 0) 65622fc5d4SAxel Lin dev_err(chip->dev, "%s: Failed to enable PWM, Error %d\n", 666173f8f4SThierry Reding pwm->label, ret); 676173f8f4SThierry Reding return ret; 686173f8f4SThierry Reding } 696173f8f4SThierry Reding 706173f8f4SThierry Reding static void ab8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) 716173f8f4SThierry Reding { 726173f8f4SThierry Reding int ret; 736173f8f4SThierry Reding 746173f8f4SThierry Reding ret = abx500_mask_and_set_register_interruptible(chip->dev, 756173f8f4SThierry Reding AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG, 7654b02347SAxel Lin 1 << (chip->base - 1), 0); 776173f8f4SThierry Reding if (ret < 0) 786173f8f4SThierry Reding dev_err(chip->dev, "%s: Failed to disable PWM, Error %d\n", 796173f8f4SThierry Reding pwm->label, ret); 806173f8f4SThierry Reding } 816173f8f4SThierry Reding 826173f8f4SThierry Reding static const struct pwm_ops ab8500_pwm_ops = { 836173f8f4SThierry Reding .config = ab8500_pwm_config, 846173f8f4SThierry Reding .enable = ab8500_pwm_enable, 856173f8f4SThierry Reding .disable = ab8500_pwm_disable, 86fa0abee9SAxel Lin .owner = THIS_MODULE, 876173f8f4SThierry Reding }; 886173f8f4SThierry Reding 893e9fe83dSBill Pemberton static int ab8500_pwm_probe(struct platform_device *pdev) 906173f8f4SThierry Reding { 916173f8f4SThierry Reding struct ab8500_pwm_chip *ab8500; 926173f8f4SThierry Reding int err; 936173f8f4SThierry Reding 946173f8f4SThierry Reding /* 956173f8f4SThierry Reding * Nothing to be done in probe, this is required to get the 966173f8f4SThierry Reding * device which is required for ab8500 read and write 976173f8f4SThierry Reding */ 98482467adSJingoo Han ab8500 = devm_kzalloc(&pdev->dev, sizeof(*ab8500), GFP_KERNEL); 99a2fc1db6SJingoo Han if (ab8500 == NULL) 1006173f8f4SThierry Reding return -ENOMEM; 1016173f8f4SThierry Reding 1026173f8f4SThierry Reding ab8500->chip.dev = &pdev->dev; 1036173f8f4SThierry Reding ab8500->chip.ops = &ab8500_pwm_ops; 1046173f8f4SThierry Reding ab8500->chip.base = pdev->id; 1056173f8f4SThierry Reding ab8500->chip.npwm = 1; 1066173f8f4SThierry Reding 1076173f8f4SThierry Reding err = pwmchip_add(&ab8500->chip); 108482467adSJingoo Han if (err < 0) 1096173f8f4SThierry Reding return err; 1106173f8f4SThierry Reding 1116173f8f4SThierry Reding dev_dbg(&pdev->dev, "pwm probe successful\n"); 1126173f8f4SThierry Reding platform_set_drvdata(pdev, ab8500); 1136173f8f4SThierry Reding 1146173f8f4SThierry Reding return 0; 1156173f8f4SThierry Reding } 1166173f8f4SThierry Reding 11777f37917SBill Pemberton static int ab8500_pwm_remove(struct platform_device *pdev) 1186173f8f4SThierry Reding { 1196173f8f4SThierry Reding struct ab8500_pwm_chip *ab8500 = platform_get_drvdata(pdev); 1206173f8f4SThierry Reding int err; 1216173f8f4SThierry Reding 1226173f8f4SThierry Reding err = pwmchip_remove(&ab8500->chip); 1236173f8f4SThierry Reding if (err < 0) 1246173f8f4SThierry Reding return err; 1256173f8f4SThierry Reding 1266173f8f4SThierry Reding dev_dbg(&pdev->dev, "pwm driver removed\n"); 1276173f8f4SThierry Reding 1286173f8f4SThierry Reding return 0; 1296173f8f4SThierry Reding } 1306173f8f4SThierry Reding 1316173f8f4SThierry Reding static struct platform_driver ab8500_pwm_driver = { 1326173f8f4SThierry Reding .driver = { 1336173f8f4SThierry Reding .name = "ab8500-pwm", 1346173f8f4SThierry Reding }, 1356173f8f4SThierry Reding .probe = ab8500_pwm_probe, 136fd109112SBill Pemberton .remove = ab8500_pwm_remove, 1376173f8f4SThierry Reding }; 1386173f8f4SThierry Reding module_platform_driver(ab8500_pwm_driver); 1396173f8f4SThierry Reding 1406173f8f4SThierry Reding MODULE_AUTHOR("Arun MURTHY <arun.murthy@stericsson.com>"); 1416173f8f4SThierry Reding MODULE_DESCRIPTION("AB8500 Pulse Width Modulation Driver"); 1426173f8f4SThierry Reding MODULE_ALIAS("platform:ab8500-pwm"); 1436173f8f4SThierry Reding MODULE_LICENSE("GPL v2"); 144