Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Johan Hovold | 16c5c02 | 2012-05-03 12:26:36 +0200 | [diff] [blame] | 2 | /* |
| 3 | * lm3533-ctrlbank.c -- LM3533 Generic Control Bank interface |
| 4 | * |
| 5 | * Copyright (C) 2011-2012 Texas Instruments |
| 6 | * |
| 7 | * Author: Johan Hovold <jhovold@gmail.com> |
Johan Hovold | 16c5c02 | 2012-05-03 12:26:36 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/module.h> |
| 12 | |
| 13 | #include <linux/mfd/lm3533.h> |
| 14 | |
| 15 | |
Johan Hovold | 6fa4b9d | 2012-05-10 19:18:29 +0200 | [diff] [blame] | 16 | #define LM3533_MAX_CURRENT_MIN 5000 |
| 17 | #define LM3533_MAX_CURRENT_MAX 29800 |
| 18 | #define LM3533_MAX_CURRENT_STEP 800 |
| 19 | |
Johan Hovold | 16c5c02 | 2012-05-03 12:26:36 +0200 | [diff] [blame] | 20 | #define LM3533_BRIGHTNESS_MAX 255 |
Johan Hovold | 16c5c02 | 2012-05-03 12:26:36 +0200 | [diff] [blame] | 21 | #define LM3533_PWM_MAX 0x3f |
| 22 | |
| 23 | #define LM3533_REG_PWM_BASE 0x14 |
| 24 | #define LM3533_REG_MAX_CURRENT_BASE 0x1f |
| 25 | #define LM3533_REG_CTRLBANK_ENABLE 0x27 |
| 26 | #define LM3533_REG_BRIGHTNESS_BASE 0x40 |
| 27 | |
| 28 | |
| 29 | static inline u8 lm3533_ctrlbank_get_reg(struct lm3533_ctrlbank *cb, u8 base) |
| 30 | { |
| 31 | return base + cb->id; |
| 32 | } |
| 33 | |
| 34 | int lm3533_ctrlbank_enable(struct lm3533_ctrlbank *cb) |
| 35 | { |
| 36 | u8 mask; |
| 37 | int ret; |
| 38 | |
| 39 | dev_dbg(cb->dev, "%s - %d\n", __func__, cb->id); |
| 40 | |
| 41 | mask = 1 << cb->id; |
| 42 | ret = lm3533_update(cb->lm3533, LM3533_REG_CTRLBANK_ENABLE, |
| 43 | mask, mask); |
| 44 | if (ret) |
| 45 | dev_err(cb->dev, "failed to enable ctrlbank %d\n", cb->id); |
| 46 | |
| 47 | return ret; |
| 48 | } |
| 49 | EXPORT_SYMBOL_GPL(lm3533_ctrlbank_enable); |
| 50 | |
| 51 | int lm3533_ctrlbank_disable(struct lm3533_ctrlbank *cb) |
| 52 | { |
| 53 | u8 mask; |
| 54 | int ret; |
| 55 | |
| 56 | dev_dbg(cb->dev, "%s - %d\n", __func__, cb->id); |
| 57 | |
| 58 | mask = 1 << cb->id; |
| 59 | ret = lm3533_update(cb->lm3533, LM3533_REG_CTRLBANK_ENABLE, 0, mask); |
| 60 | if (ret) |
| 61 | dev_err(cb->dev, "failed to disable ctrlbank %d\n", cb->id); |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | EXPORT_SYMBOL_GPL(lm3533_ctrlbank_disable); |
| 66 | |
Johan Hovold | 6fa4b9d | 2012-05-10 19:18:29 +0200 | [diff] [blame] | 67 | /* |
| 68 | * Full-scale current. |
| 69 | * |
| 70 | * imax 5000 - 29800 uA (800 uA step) |
| 71 | */ |
| 72 | int lm3533_ctrlbank_set_max_current(struct lm3533_ctrlbank *cb, u16 imax) |
| 73 | { |
| 74 | u8 reg; |
| 75 | u8 val; |
| 76 | int ret; |
| 77 | |
| 78 | if (imax < LM3533_MAX_CURRENT_MIN || imax > LM3533_MAX_CURRENT_MAX) |
| 79 | return -EINVAL; |
| 80 | |
| 81 | val = (imax - LM3533_MAX_CURRENT_MIN) / LM3533_MAX_CURRENT_STEP; |
| 82 | |
| 83 | reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_MAX_CURRENT_BASE); |
| 84 | ret = lm3533_write(cb->lm3533, reg, val); |
| 85 | if (ret) |
| 86 | dev_err(cb->dev, "failed to set max current\n"); |
| 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(lm3533_ctrlbank_set_max_current); |
| 91 | |
Johan Hovold | 16c5c02 | 2012-05-03 12:26:36 +0200 | [diff] [blame] | 92 | #define lm3533_ctrlbank_set(_name, _NAME) \ |
| 93 | int lm3533_ctrlbank_set_##_name(struct lm3533_ctrlbank *cb, u8 val) \ |
| 94 | { \ |
| 95 | u8 reg; \ |
| 96 | int ret; \ |
| 97 | \ |
| 98 | if (val > LM3533_##_NAME##_MAX) \ |
| 99 | return -EINVAL; \ |
| 100 | \ |
| 101 | reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_##_NAME##_BASE); \ |
| 102 | ret = lm3533_write(cb->lm3533, reg, val); \ |
| 103 | if (ret) \ |
| 104 | dev_err(cb->dev, "failed to set " #_name "\n"); \ |
| 105 | \ |
| 106 | return ret; \ |
| 107 | } \ |
| 108 | EXPORT_SYMBOL_GPL(lm3533_ctrlbank_set_##_name); |
| 109 | |
| 110 | #define lm3533_ctrlbank_get(_name, _NAME) \ |
| 111 | int lm3533_ctrlbank_get_##_name(struct lm3533_ctrlbank *cb, u8 *val) \ |
| 112 | { \ |
| 113 | u8 reg; \ |
| 114 | int ret; \ |
| 115 | \ |
| 116 | reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_##_NAME##_BASE); \ |
| 117 | ret = lm3533_read(cb->lm3533, reg, val); \ |
| 118 | if (ret) \ |
| 119 | dev_err(cb->dev, "failed to get " #_name "\n"); \ |
| 120 | \ |
| 121 | return ret; \ |
| 122 | } \ |
| 123 | EXPORT_SYMBOL_GPL(lm3533_ctrlbank_get_##_name); |
| 124 | |
| 125 | lm3533_ctrlbank_set(brightness, BRIGHTNESS); |
| 126 | lm3533_ctrlbank_get(brightness, BRIGHTNESS); |
| 127 | |
| 128 | /* |
Johan Hovold | 16c5c02 | 2012-05-03 12:26:36 +0200 | [diff] [blame] | 129 | * PWM-input control mask: |
| 130 | * |
| 131 | * bit 5 - PWM-input enabled in Zone 4 |
| 132 | * bit 4 - PWM-input enabled in Zone 3 |
| 133 | * bit 3 - PWM-input enabled in Zone 2 |
| 134 | * bit 2 - PWM-input enabled in Zone 1 |
| 135 | * bit 1 - PWM-input enabled in Zone 0 |
| 136 | * bit 0 - PWM-input enabled |
| 137 | */ |
| 138 | lm3533_ctrlbank_set(pwm, PWM); |
| 139 | lm3533_ctrlbank_get(pwm, PWM); |
| 140 | |
| 141 | |
| 142 | MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>"); |
| 143 | MODULE_DESCRIPTION("LM3533 Control Bank interface"); |
| 144 | MODULE_LICENSE("GPL"); |