Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Driver for TWL4030/6030 Pulse Width Modulator used as LED driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2012 Texas Instruments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * This driver is a complete rewrite of the former pwm-twl6030.c authorded by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Hemanth V <hemanthv@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/pwm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/mfd/twl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * This driver handles the PWM driven LED terminals of TWL4030 and TWL6030.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * To generate the signal on TWL4030:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *  - LEDA uses PWMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *  - LEDB uses PWMB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * TWL6030 has one LED pin with dedicated LEDPWM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define TWL4030_LED_MAX		0x7f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define TWL6030_LED_MAX		0xff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /* Registers, bits and macro for TWL4030 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define TWL4030_LEDEN_REG	0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define TWL4030_PWMA_REG	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define TWL4030_LEDXON		(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define TWL4030_LEDXPWM		(1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define TWL4030_LED_PINS	(TWL4030_LEDXON | TWL4030_LEDXPWM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define TWL4030_LED_TOGGLE(led, x)	((x) << (led))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /* Register, bits and macro for TWL6030 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define TWL6030_LED_PWM_CTRL1	0xf4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define TWL6030_LED_PWM_CTRL2	0xf5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define TWL6040_LED_MODE_HW	0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define TWL6040_LED_MODE_ON	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define TWL6040_LED_MODE_OFF	0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define TWL6040_LED_MODE_MASK	0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) struct twl_pwmled_chip {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct pwm_chip chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static inline struct twl_pwmled_chip *to_twl(struct pwm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return container_of(chip, struct twl_pwmled_chip, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static int twl4030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 			      int duty_ns, int period_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int duty_cycle = DIV_ROUND_UP(duty_ns * TWL4030_LED_MAX, period_ns) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	u8 pwm_config[2] = { 1, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	int base, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	 * To configure the duty period:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	 * On-cycle is set to 1 (the minimum allowed value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	 * The off time of 0 is not configurable, so the mapping is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	 * 0 -> off cycle = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	 * 1 -> off cycle = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	 * 2 -> off cycle = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	 * 126 - > off cycle 127,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	 * 127 - > off cycle 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	 * When on cycle == off cycle the PWM will be always on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (duty_cycle == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		duty_cycle = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	else if (duty_cycle > TWL4030_LED_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		duty_cycle = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	base = pwm->hwpwm * 2 + TWL4030_PWMA_REG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	pwm_config[1] = duty_cycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	ret = twl_i2c_write(TWL4030_MODULE_LED, pwm_config, base, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static int twl4030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct twl_pwmled_chip *twl = to_twl(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	mutex_lock(&twl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	val |= TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	mutex_unlock(&twl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static void twl4030_pwmled_disable(struct pwm_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 				   struct pwm_device *pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct twl_pwmled_chip *twl = to_twl(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	mutex_lock(&twl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	val &= ~TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	mutex_unlock(&twl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int twl6030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			      int duty_ns, int period_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	int duty_cycle = (duty_ns * TWL6030_LED_MAX) / period_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	u8 on_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	on_time = duty_cycle & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, on_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			       TWL6030_LED_PWM_CTRL1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int twl6030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	struct twl_pwmled_chip *twl = to_twl(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	mutex_lock(&twl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	val &= ~TWL6040_LED_MODE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	val |= TWL6040_LED_MODE_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	mutex_unlock(&twl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static void twl6030_pwmled_disable(struct pwm_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 				   struct pwm_device *pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct twl_pwmled_chip *twl = to_twl(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	mutex_lock(&twl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	val &= ~TWL6040_LED_MODE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	val |= TWL6040_LED_MODE_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	mutex_unlock(&twl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static int twl6030_pwmled_request(struct pwm_chip *chip, struct pwm_device *pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	struct twl_pwmled_chip *twl = to_twl(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	mutex_lock(&twl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	val &= ~TWL6040_LED_MODE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	val |= TWL6040_LED_MODE_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	mutex_unlock(&twl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static void twl6030_pwmled_free(struct pwm_chip *chip, struct pwm_device *pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct twl_pwmled_chip *twl = to_twl(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	mutex_lock(&twl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	val &= ~TWL6040_LED_MODE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	val |= TWL6040_LED_MODE_HW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	mutex_unlock(&twl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static const struct pwm_ops twl4030_pwmled_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	.enable = twl4030_pwmled_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	.disable = twl4030_pwmled_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	.config = twl4030_pwmled_config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static const struct pwm_ops twl6030_pwmled_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	.enable = twl6030_pwmled_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	.disable = twl6030_pwmled_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	.config = twl6030_pwmled_config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	.request = twl6030_pwmled_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	.free = twl6030_pwmled_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static int twl_pwmled_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	struct twl_pwmled_chip *twl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (!twl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (twl_class_is_4030()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		twl->chip.ops = &twl4030_pwmled_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		twl->chip.npwm = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		twl->chip.ops = &twl6030_pwmled_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		twl->chip.npwm = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	twl->chip.dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	twl->chip.base = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	mutex_init(&twl->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	ret = pwmchip_add(&twl->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	platform_set_drvdata(pdev, twl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static int twl_pwmled_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	struct twl_pwmled_chip *twl = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	return pwmchip_remove(&twl->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static const struct of_device_id twl_pwmled_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	{ .compatible = "ti,twl4030-pwmled" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	{ .compatible = "ti,twl6030-pwmled" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) MODULE_DEVICE_TABLE(of, twl_pwmled_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static struct platform_driver twl_pwmled_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		.name = "twl-pwmled",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		.of_match_table = of_match_ptr(twl_pwmled_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	.probe = twl_pwmled_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	.remove = twl_pwmled_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) module_platform_driver(twl_pwmled_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030 LED outputs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) MODULE_ALIAS("platform:twl-pwmled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) MODULE_LICENSE("GPL");