Johan Hovold | 16c5c02 | 2012-05-03 12:26:36 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * lm3533-ctrlbank.c -- LM3533 Generic Control Bank interface |
| 3 | * |
| 4 | * Copyright (C) 2011-2012 Texas Instruments |
| 5 | * |
| 6 | * Author: Johan Hovold <jhovold@gmail.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/module.h> |
| 16 | |
| 17 | #include <linux/mfd/lm3533.h> |
| 18 | |
| 19 | |
| 20 | #define LM3533_BRIGHTNESS_MAX 255 |
| 21 | #define LM3533_MAX_CURRENT_MAX 31 |
| 22 | #define LM3533_PWM_MAX 0x3f |
| 23 | |
| 24 | #define LM3533_REG_PWM_BASE 0x14 |
| 25 | #define LM3533_REG_MAX_CURRENT_BASE 0x1f |
| 26 | #define LM3533_REG_CTRLBANK_ENABLE 0x27 |
| 27 | #define LM3533_REG_BRIGHTNESS_BASE 0x40 |
| 28 | |
| 29 | |
| 30 | static inline u8 lm3533_ctrlbank_get_reg(struct lm3533_ctrlbank *cb, u8 base) |
| 31 | { |
| 32 | return base + cb->id; |
| 33 | } |
| 34 | |
| 35 | int lm3533_ctrlbank_enable(struct lm3533_ctrlbank *cb) |
| 36 | { |
| 37 | u8 mask; |
| 38 | int ret; |
| 39 | |
| 40 | dev_dbg(cb->dev, "%s - %d\n", __func__, cb->id); |
| 41 | |
| 42 | mask = 1 << cb->id; |
| 43 | ret = lm3533_update(cb->lm3533, LM3533_REG_CTRLBANK_ENABLE, |
| 44 | mask, mask); |
| 45 | if (ret) |
| 46 | dev_err(cb->dev, "failed to enable ctrlbank %d\n", cb->id); |
| 47 | |
| 48 | return ret; |
| 49 | } |
| 50 | EXPORT_SYMBOL_GPL(lm3533_ctrlbank_enable); |
| 51 | |
| 52 | int lm3533_ctrlbank_disable(struct lm3533_ctrlbank *cb) |
| 53 | { |
| 54 | u8 mask; |
| 55 | int ret; |
| 56 | |
| 57 | dev_dbg(cb->dev, "%s - %d\n", __func__, cb->id); |
| 58 | |
| 59 | mask = 1 << cb->id; |
| 60 | ret = lm3533_update(cb->lm3533, LM3533_REG_CTRLBANK_ENABLE, 0, mask); |
| 61 | if (ret) |
| 62 | dev_err(cb->dev, "failed to disable ctrlbank %d\n", cb->id); |
| 63 | |
| 64 | return ret; |
| 65 | } |
| 66 | EXPORT_SYMBOL_GPL(lm3533_ctrlbank_disable); |
| 67 | |
| 68 | #define lm3533_ctrlbank_set(_name, _NAME) \ |
| 69 | int lm3533_ctrlbank_set_##_name(struct lm3533_ctrlbank *cb, u8 val) \ |
| 70 | { \ |
| 71 | u8 reg; \ |
| 72 | int ret; \ |
| 73 | \ |
| 74 | if (val > LM3533_##_NAME##_MAX) \ |
| 75 | return -EINVAL; \ |
| 76 | \ |
| 77 | reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_##_NAME##_BASE); \ |
| 78 | ret = lm3533_write(cb->lm3533, reg, val); \ |
| 79 | if (ret) \ |
| 80 | dev_err(cb->dev, "failed to set " #_name "\n"); \ |
| 81 | \ |
| 82 | return ret; \ |
| 83 | } \ |
| 84 | EXPORT_SYMBOL_GPL(lm3533_ctrlbank_set_##_name); |
| 85 | |
| 86 | #define lm3533_ctrlbank_get(_name, _NAME) \ |
| 87 | int lm3533_ctrlbank_get_##_name(struct lm3533_ctrlbank *cb, u8 *val) \ |
| 88 | { \ |
| 89 | u8 reg; \ |
| 90 | int ret; \ |
| 91 | \ |
| 92 | reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_##_NAME##_BASE); \ |
| 93 | ret = lm3533_read(cb->lm3533, reg, val); \ |
| 94 | if (ret) \ |
| 95 | dev_err(cb->dev, "failed to get " #_name "\n"); \ |
| 96 | \ |
| 97 | return ret; \ |
| 98 | } \ |
| 99 | EXPORT_SYMBOL_GPL(lm3533_ctrlbank_get_##_name); |
| 100 | |
| 101 | lm3533_ctrlbank_set(brightness, BRIGHTNESS); |
| 102 | lm3533_ctrlbank_get(brightness, BRIGHTNESS); |
| 103 | |
| 104 | /* |
| 105 | * Full scale current. |
| 106 | * |
| 107 | * Imax = 5 + val * 0.8 mA, e.g.: |
| 108 | * |
| 109 | * 0 - 5 mA |
| 110 | * ... |
| 111 | * 19 - 20.2 mA (default) |
| 112 | * ... |
| 113 | * 31 - 29.8 mA |
| 114 | */ |
| 115 | lm3533_ctrlbank_set(max_current, MAX_CURRENT); |
| 116 | lm3533_ctrlbank_get(max_current, MAX_CURRENT); |
| 117 | |
| 118 | /* |
| 119 | * PWM-input control mask: |
| 120 | * |
| 121 | * bit 5 - PWM-input enabled in Zone 4 |
| 122 | * bit 4 - PWM-input enabled in Zone 3 |
| 123 | * bit 3 - PWM-input enabled in Zone 2 |
| 124 | * bit 2 - PWM-input enabled in Zone 1 |
| 125 | * bit 1 - PWM-input enabled in Zone 0 |
| 126 | * bit 0 - PWM-input enabled |
| 127 | */ |
| 128 | lm3533_ctrlbank_set(pwm, PWM); |
| 129 | lm3533_ctrlbank_get(pwm, PWM); |
| 130 | |
| 131 | |
| 132 | MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>"); |
| 133 | MODULE_DESCRIPTION("LM3533 Control Bank interface"); |
| 134 | MODULE_LICENSE("GPL"); |