Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright 2015 Texas Instruments
0003 // Copyright 2018 Sebastian Reichel
0004 // Copyright 2018 Pavel Machek <pavel@ucw.cz>
0005 // TI LMU LED common framework, based on previous work from
0006 // Milo Kim <milo.kim@ti.com>
0007 
0008 #include <linux/bitops.h>
0009 #include <linux/err.h>
0010 #include <linux/of_device.h>
0011 
0012 #include <linux/leds-ti-lmu-common.h>
0013 
0014 static const unsigned int ramp_table[16] = {2048, 262000, 524000, 1049000,
0015                 2090000, 4194000, 8389000, 16780000, 33550000,
0016                 41940000, 50330000, 58720000, 67110000,
0017                 83880000, 100660000, 117440000};
0018 
0019 static int ti_lmu_common_update_brightness(struct ti_lmu_bank *lmu_bank,
0020                        int brightness)
0021 {
0022     struct regmap *regmap = lmu_bank->regmap;
0023     u8 reg, val;
0024     int ret;
0025 
0026     /*
0027      * Brightness register update
0028      *
0029      * 11 bit dimming: update LSB bits and write MSB byte.
0030      *         MSB brightness should be shifted.
0031      *  8 bit dimming: write MSB byte.
0032      */
0033     if (lmu_bank->max_brightness == MAX_BRIGHTNESS_11BIT) {
0034         reg = lmu_bank->lsb_brightness_reg;
0035         ret = regmap_update_bits(regmap, reg,
0036                      LMU_11BIT_LSB_MASK,
0037                      brightness);
0038         if (ret)
0039             return ret;
0040 
0041         val = brightness >> LMU_11BIT_MSB_SHIFT;
0042     } else {
0043         val = brightness;
0044     }
0045 
0046     reg = lmu_bank->msb_brightness_reg;
0047 
0048     return regmap_write(regmap, reg, val);
0049 }
0050 
0051 int ti_lmu_common_set_brightness(struct ti_lmu_bank *lmu_bank, int brightness)
0052 {
0053     return ti_lmu_common_update_brightness(lmu_bank, brightness);
0054 }
0055 EXPORT_SYMBOL(ti_lmu_common_set_brightness);
0056 
0057 static unsigned int ti_lmu_common_convert_ramp_to_index(unsigned int usec)
0058 {
0059     int size = ARRAY_SIZE(ramp_table);
0060     int i;
0061 
0062     if (usec <= ramp_table[0])
0063         return 0;
0064 
0065     if (usec > ramp_table[size - 1])
0066         return size - 1;
0067 
0068     for (i = 1; i < size; i++) {
0069         if (usec == ramp_table[i])
0070             return i;
0071 
0072         /* Find an approximate index by looking up the table */
0073         if (usec > ramp_table[i - 1] && usec < ramp_table[i]) {
0074             if (usec - ramp_table[i - 1] < ramp_table[i] - usec)
0075                 return i - 1;
0076             else
0077                 return i;
0078         }
0079     }
0080 
0081     return 0;
0082 }
0083 
0084 int ti_lmu_common_set_ramp(struct ti_lmu_bank *lmu_bank)
0085 {
0086     struct regmap *regmap = lmu_bank->regmap;
0087     u8 ramp, ramp_up, ramp_down;
0088 
0089     if (lmu_bank->ramp_up_usec == 0 && lmu_bank->ramp_down_usec == 0) {
0090         ramp_up = 0;
0091         ramp_down = 0;
0092     } else {
0093         ramp_up = ti_lmu_common_convert_ramp_to_index(lmu_bank->ramp_up_usec);
0094         ramp_down = ti_lmu_common_convert_ramp_to_index(lmu_bank->ramp_down_usec);
0095     }
0096 
0097     ramp = (ramp_up << 4) | ramp_down;
0098 
0099     return regmap_write(regmap, lmu_bank->runtime_ramp_reg, ramp);
0100 
0101 }
0102 EXPORT_SYMBOL(ti_lmu_common_set_ramp);
0103 
0104 int ti_lmu_common_get_ramp_params(struct device *dev,
0105                   struct fwnode_handle *child,
0106                   struct ti_lmu_bank *lmu_data)
0107 {
0108     int ret;
0109 
0110     ret = fwnode_property_read_u32(child, "ramp-up-us",
0111                  &lmu_data->ramp_up_usec);
0112     if (ret)
0113         dev_warn(dev, "ramp-up-us property missing\n");
0114 
0115 
0116     ret = fwnode_property_read_u32(child, "ramp-down-us",
0117                  &lmu_data->ramp_down_usec);
0118     if (ret)
0119         dev_warn(dev, "ramp-down-us property missing\n");
0120 
0121     return 0;
0122 }
0123 EXPORT_SYMBOL(ti_lmu_common_get_ramp_params);
0124 
0125 int ti_lmu_common_get_brt_res(struct device *dev, struct fwnode_handle *child,
0126                   struct ti_lmu_bank *lmu_data)
0127 {
0128     int ret;
0129 
0130     ret = device_property_read_u32(dev, "ti,brightness-resolution",
0131                        &lmu_data->max_brightness);
0132     if (ret)
0133         ret = fwnode_property_read_u32(child,
0134                            "ti,brightness-resolution",
0135                            &lmu_data->max_brightness);
0136     if (lmu_data->max_brightness <= 0) {
0137         lmu_data->max_brightness = MAX_BRIGHTNESS_8BIT;
0138         return ret;
0139     }
0140 
0141     if (lmu_data->max_brightness > MAX_BRIGHTNESS_11BIT)
0142             lmu_data->max_brightness = MAX_BRIGHTNESS_11BIT;
0143 
0144 
0145     return 0;
0146 }
0147 EXPORT_SYMBOL(ti_lmu_common_get_brt_res);
0148 
0149 MODULE_DESCRIPTION("TI LMU common LED framework");
0150 MODULE_AUTHOR("Sebastian Reichel");
0151 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
0152 MODULE_LICENSE("GPL v2");
0153 MODULE_ALIAS("ti-lmu-led-common");