blob: e009b6d1791546f9ed330e96ea0c49a8dd5aa578 [file] [log] [blame]
Dan Murphy11e1bbc2019-06-05 07:56:34 -05001// SPDX-License-Identifier: GPL-2.0
2// TI LM36274 LED chip family driver
Alexander A. Klimovc5437332020-07-13 16:51:15 +02003// Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com/
Dan Murphy11e1bbc2019-06-05 07:56:34 -05004
5#include <linux/bitops.h>
6#include <linux/device.h>
7#include <linux/err.h>
8#include <linux/leds.h>
9#include <linux/leds-ti-lmu-common.h>
Andy Shevchenkoe2e8e4e82021-05-10 12:50:34 +030010#include <linux/mod_devicetable.h>
Dan Murphy11e1bbc2019-06-05 07:56:34 -050011#include <linux/module.h>
Dan Murphy11e1bbc2019-06-05 07:56:34 -050012#include <linux/platform_device.h>
Andy Shevchenkoe90abb92021-05-30 23:32:28 +030013#include <linux/property.h>
Dan Murphy11e1bbc2019-06-05 07:56:34 -050014
15#include <linux/mfd/ti-lmu.h>
16#include <linux/mfd/ti-lmu-register.h>
17
18#include <uapi/linux/uleds.h>
19
20#define LM36274_MAX_STRINGS 4
21#define LM36274_BL_EN BIT(4)
22
23/**
24 * struct lm36274
25 * @pdev: platform device
26 * @led_dev: led class device
27 * @lmu_data: Register and setting values for common code
28 * @regmap: Devices register map
29 * @dev: Pointer to the devices device struct
Dan Murphy9adc8af2020-09-22 14:06:38 -050030 * @led_sources: The LED strings supported in this array
31 * @num_leds: Number of LED strings are supported in this array
Dan Murphy11e1bbc2019-06-05 07:56:34 -050032 */
33struct lm36274 {
34 struct platform_device *pdev;
35 struct led_classdev led_dev;
36 struct ti_lmu_bank lmu_data;
37 struct regmap *regmap;
38 struct device *dev;
39
40 u32 led_sources[LM36274_MAX_STRINGS];
41 int num_leds;
42};
43
44static int lm36274_brightness_set(struct led_classdev *led_cdev,
Marek Behúnd3ab9632020-09-19 20:02:56 +020045 enum led_brightness brt_val)
Dan Murphy11e1bbc2019-06-05 07:56:34 -050046{
Marek Behúnd3ab9632020-09-19 20:02:56 +020047 struct lm36274 *chip = container_of(led_cdev, struct lm36274, led_dev);
Dan Murphy11e1bbc2019-06-05 07:56:34 -050048
Marek Behúnd3ab9632020-09-19 20:02:56 +020049 return ti_lmu_common_set_brightness(&chip->lmu_data, brt_val);
Dan Murphy11e1bbc2019-06-05 07:56:34 -050050}
51
Marek Behúnd3ab9632020-09-19 20:02:56 +020052static int lm36274_init(struct lm36274 *chip)
Dan Murphy11e1bbc2019-06-05 07:56:34 -050053{
54 int enable_val = 0;
55 int i;
56
Marek Behúnd3ab9632020-09-19 20:02:56 +020057 for (i = 0; i < chip->num_leds; i++)
58 enable_val |= (1 << chip->led_sources[i]);
Dan Murphy11e1bbc2019-06-05 07:56:34 -050059
60 if (!enable_val) {
Marek Behúnd3ab9632020-09-19 20:02:56 +020061 dev_err(chip->dev, "No LEDs were enabled\n");
Dan Murphy11e1bbc2019-06-05 07:56:34 -050062 return -EINVAL;
63 }
64
65 enable_val |= LM36274_BL_EN;
66
Marek Behúnd3ab9632020-09-19 20:02:56 +020067 return regmap_write(chip->regmap, LM36274_REG_BL_EN, enable_val);
Dan Murphy11e1bbc2019-06-05 07:56:34 -050068}
69
Marek Behún1aeef382020-09-19 20:02:58 +020070static int lm36274_parse_dt(struct lm36274 *chip,
71 struct led_init_data *init_data)
Dan Murphy11e1bbc2019-06-05 07:56:34 -050072{
Marek Behún5c0d20a2020-09-19 20:03:00 +020073 struct device *dev = chip->dev;
Marek Behún1aeef382020-09-19 20:02:58 +020074 struct fwnode_handle *child;
Marek Behúna448fcf2020-09-19 20:02:57 +020075 int ret;
Dan Murphy11e1bbc2019-06-05 07:56:34 -050076
77 /* There should only be 1 node */
Marek Behúna448fcf2020-09-19 20:02:57 +020078 if (device_get_child_node_count(dev) != 1)
Dan Murphy11e1bbc2019-06-05 07:56:34 -050079 return -EINVAL;
80
Marek Behúna448fcf2020-09-19 20:02:57 +020081 child = device_get_next_child_node(dev, NULL);
Dan Murphy11e1bbc2019-06-05 07:56:34 -050082
Marek Behún1aeef382020-09-19 20:02:58 +020083 init_data->fwnode = child;
84 init_data->devicename = chip->pdev->name;
85 /* for backwards compatibility when `label` property is not present */
86 init_data->default_label = ":";
Dan Murphy11e1bbc2019-06-05 07:56:34 -050087
Marek Behúna448fcf2020-09-19 20:02:57 +020088 chip->num_leds = fwnode_property_count_u32(child, "led-sources");
Marek Behún1aeef382020-09-19 20:02:58 +020089 if (chip->num_leds <= 0) {
90 ret = -ENODEV;
91 goto err;
92 }
Dan Murphy11e1bbc2019-06-05 07:56:34 -050093
Marek Behúna448fcf2020-09-19 20:02:57 +020094 ret = fwnode_property_read_u32_array(child, "led-sources",
95 chip->led_sources, chip->num_leds);
96 if (ret) {
97 dev_err(dev, "led-sources property missing\n");
Marek Behún1aeef382020-09-19 20:02:58 +020098 goto err;
Dan Murphy11e1bbc2019-06-05 07:56:34 -050099 }
100
Dan Murphy11e1bbc2019-06-05 07:56:34 -0500101 return 0;
Marek Behún1aeef382020-09-19 20:02:58 +0200102err:
103 fwnode_handle_put(child);
104 return ret;
Dan Murphy11e1bbc2019-06-05 07:56:34 -0500105}
106
107static int lm36274_probe(struct platform_device *pdev)
108{
109 struct ti_lmu *lmu = dev_get_drvdata(pdev->dev.parent);
Marek Behún1aeef382020-09-19 20:02:58 +0200110 struct led_init_data init_data = {};
Marek Behúnd3ab9632020-09-19 20:02:56 +0200111 struct lm36274 *chip;
Dan Murphy11e1bbc2019-06-05 07:56:34 -0500112 int ret;
113
Marek Behúnd3ab9632020-09-19 20:02:56 +0200114 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
115 if (!chip)
Dan Murphy11e1bbc2019-06-05 07:56:34 -0500116 return -ENOMEM;
117
Marek Behúnd3ab9632020-09-19 20:02:56 +0200118 chip->pdev = pdev;
Marek Behún5c0d20a2020-09-19 20:03:00 +0200119 chip->dev = &pdev->dev;
Marek Behúnd3ab9632020-09-19 20:02:56 +0200120 chip->regmap = lmu->regmap;
121 platform_set_drvdata(pdev, chip);
Dan Murphy11e1bbc2019-06-05 07:56:34 -0500122
Marek Behún1aeef382020-09-19 20:02:58 +0200123 ret = lm36274_parse_dt(chip, &init_data);
Dan Murphy11e1bbc2019-06-05 07:56:34 -0500124 if (ret) {
Marek Behúnd3ab9632020-09-19 20:02:56 +0200125 dev_err(chip->dev, "Failed to parse DT node\n");
Dan Murphy11e1bbc2019-06-05 07:56:34 -0500126 return ret;
127 }
128
Marek Behúnd3ab9632020-09-19 20:02:56 +0200129 ret = lm36274_init(chip);
Dan Murphy11e1bbc2019-06-05 07:56:34 -0500130 if (ret) {
Andy Shevchenko3c5f6552021-05-10 12:50:33 +0300131 fwnode_handle_put(init_data.fwnode);
Marek Behúnd3ab9632020-09-19 20:02:56 +0200132 dev_err(chip->dev, "Failed to init the device\n");
Dan Murphy11e1bbc2019-06-05 07:56:34 -0500133 return ret;
134 }
135
Marek Behún48445672020-09-19 20:02:59 +0200136 chip->lmu_data.regmap = chip->regmap;
137 chip->lmu_data.max_brightness = MAX_BRIGHTNESS_11BIT;
138 chip->lmu_data.msb_brightness_reg = LM36274_REG_BRT_MSB;
139 chip->lmu_data.lsb_brightness_reg = LM36274_REG_BRT_LSB;
140
141 chip->led_dev.max_brightness = MAX_BRIGHTNESS_11BIT;
142 chip->led_dev.brightness_set_blocking = lm36274_brightness_set;
143
Marek Behún60bbd9d2020-09-19 20:03:01 +0200144 ret = devm_led_classdev_register_ext(chip->dev, &chip->led_dev,
145 &init_data);
Marek Behún1aeef382020-09-19 20:02:58 +0200146 if (ret)
147 dev_err(chip->dev, "Failed to register LED for node %pfw\n",
148 init_data.fwnode);
149
150 fwnode_handle_put(init_data.fwnode);
151
152 return ret;
Johan Hovolda0972ff2020-06-01 15:39:48 +0200153}
154
Dan Murphy11e1bbc2019-06-05 07:56:34 -0500155static const struct of_device_id of_lm36274_leds_match[] = {
156 { .compatible = "ti,lm36274-backlight", },
157 {},
158};
159MODULE_DEVICE_TABLE(of, of_lm36274_leds_match);
160
161static struct platform_driver lm36274_driver = {
162 .probe = lm36274_probe,
Dan Murphy11e1bbc2019-06-05 07:56:34 -0500163 .driver = {
164 .name = "lm36274-leds",
Dan Murphy9adc8af2020-09-22 14:06:38 -0500165 .of_match_table = of_lm36274_leds_match,
Dan Murphy11e1bbc2019-06-05 07:56:34 -0500166 },
167};
168module_platform_driver(lm36274_driver)
169
170MODULE_DESCRIPTION("Texas Instruments LM36274 LED driver");
171MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
172MODULE_LICENSE("GPL v2");