blob: 2cefe6b7da4abb05e8eae0bf4af577394872e07b [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Duncan Laurie0cf0d142013-07-15 09:11:21 -07003
4#include <console/console.h>
5#include <delay.h>
6#include <device/device.h>
7#include <device/smbus.h>
8#include <device/pci.h>
9#include "chip.h"
10
11/* Chip commands */
12#define RTD2132_COMMAND 0x01
13#define RTD2132_DATA 0x00
14#define RTD2132_FIRMWARE 0x80
15#define RTD2132_FIRMWARE_START 0x00
16#define RTD2132_FIRMWARE_STOP 0x01
17
Aaron Durbinebad1762013-08-14 11:27:40 -050018/* Panel Power Sequence Timing Registers. */
19#define RTD2132_COMMAND_PWR_SEQ_T1 0x32 /* 1ms units. */
20#define RTD2132_COMMAND_PWR_SEQ_T2 0x33 /* 4ms units. */
21#define RTD2132_COMMAND_PWR_SEQ_T3 0x34 /* 1ms units. */
22#define RTD2132_COMMAND_PWR_SEQ_T4 0x35 /* 1ms units. */
23#define RTD2132_COMMAND_PWR_SEQ_T5 0x36 /* 4ms units. */
24#define RTD2132_COMMAND_PWR_SEQ_T6 0x37 /* 1ms units. */
25#define RTD2132_COMMAND_PWR_SEQ_T7 0x38 /* 4ms units. */
26
Duncan Laurie0cf0d142013-07-15 09:11:21 -070027/* Spread spectrum configuration */
28#define RTD2132_COMMAND_SSCG_CONFIG_0 0x39
29#define RTD2132_SSCG_ENABLE 0xa0
30#define RTD2132_SSCG_DISABLE 0x20
31#define RTD2132_COMMAND_SSCG_CONFIG_1 0x3a
32#define RTD2132_SSCG_CONFIG_DISABLED 0x01 /* DISABLED */
33#define RTD2132_SSCG_CONFIG_0_5 0x07 /* 0.5% */
34#define RTD2132_SSCG_CONFIG_1_0 0x0f /* 1.0% */
35#define RTD2132_SSCG_CONFIG_1_5 0x16 /* 1.5% */
36
Aaron Durbinebad1762013-08-14 11:27:40 -050037/* LVDS Swap */
38#define RTD2132_COMMAND_LVDS_SWAP 0x3b
39#define RTD2132_LVDS_SWAP_DUAL 0x80
40#define RTD2132_LVDS_SWAP_NORMAL 0x04
41#define RTD2132_LVDS_SWAP_MIRROR 0x14
42#define RTD2132_LVDS_SWAP_P_N 0x24
43#define RTD2132_LVDS_SWAP_MIRROR_P_N 0x34
44#define RTD2132_LVDS_SWAP_R_L 0x0c
45
Duncan Laurie0cf0d142013-07-15 09:11:21 -070046/* Configuration values from devicetree */
47#define RTD2132_SSCG_PERCENT_0_0 0x00 /* DISABLED */
48#define RTD2132_SSCG_PERCENT_0_5 0x05 /* 0.5% */
49#define RTD2132_SSCG_PERCENT_1_0 0x10 /* 1.0% */
50#define RTD2132_SSCG_PERCENT_1_5 0x15 /* 1.5% */
51
Aaron Durbinebad1762013-08-14 11:27:40 -050052#define RTD2132_LVDS_SWAP_CFG_DUAL 0x80
53#define RTD2132_LVDS_SWAP_CFG_NORMAL 0x00
54#define RTD2132_LVDS_SWAP_CFG_MIRROR 0x01
55#define RTD2132_LVDS_SWAP_CFG_P_N 0x02
56#define RTD2132_LVDS_SWAP_CFG_MIRROR_P_N 0x03
57#define RTD2132_LVDS_SWAP_CFG_R_L 0x04
58
59#define RTD2132_DEBUG_REG 0
60
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110061static void rtd2132_write_reg(struct device *dev, u8 reg, u8 value)
Aaron Durbinebad1762013-08-14 11:27:40 -050062{
63 if (RTD2132_DEBUG_REG)
64 printk(BIOS_DEBUG, "RTD2132 0x%02x <- 0x%02x\n", reg, value);
65 smbus_write_byte(dev, RTD2132_COMMAND, reg);
66 smbus_write_byte(dev, RTD2132_DATA, value);
67}
68
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110069static void rtd2132_firmware_stop(struct device *dev)
Duncan Laurie0cf0d142013-07-15 09:11:21 -070070{
71 smbus_write_byte(dev, RTD2132_FIRMWARE, RTD2132_FIRMWARE_STOP);
Aaron Durbinebad1762013-08-14 11:27:40 -050072 mdelay(60);
Duncan Laurie0cf0d142013-07-15 09:11:21 -070073}
74
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110075static void rtd2132_firmware_start(struct device *dev)
Duncan Laurie0cf0d142013-07-15 09:11:21 -070076{
77 smbus_write_byte(dev, RTD2132_FIRMWARE, RTD2132_FIRMWARE_START);
78}
79
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110080static void rtd2132_pps(struct device *dev, struct drivers_i2c_rtd2132_config *cfg)
Aaron Durbinebad1762013-08-14 11:27:40 -050081{
82 /* T2, T5, and T7 register values are in units of 4ms. */
83 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T1, cfg->t1);
84 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T2, cfg->t2 / 4);
85 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T3, cfg->t3);
86 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T4, cfg->t4);
87 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T5, cfg->t5 / 4);
88 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T6, cfg->t6);
89 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T7, cfg->t7 / 4);
90}
91
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110092static void rtd2132_sscg_enable(struct device *dev, u8 sscg_percent)
Duncan Laurie0cf0d142013-07-15 09:11:21 -070093{
94 /* SSCG_Config_0 */
Aaron Durbinebad1762013-08-14 11:27:40 -050095 rtd2132_write_reg(dev, RTD2132_COMMAND_SSCG_CONFIG_0,
96 RTD2132_SSCG_ENABLE);
Duncan Laurie0cf0d142013-07-15 09:11:21 -070097
98 /* SSCG_Config_1 */
Aaron Durbinebad1762013-08-14 11:27:40 -050099 rtd2132_write_reg(dev, RTD2132_COMMAND_SSCG_CONFIG_1, sscg_percent);
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700100}
101
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100102static void rtd2132_sscg_disable(struct device *dev)
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700103{
104 /* SSCG_Config_0 */
Aaron Durbinebad1762013-08-14 11:27:40 -0500105 rtd2132_write_reg(dev, RTD2132_COMMAND_SSCG_CONFIG_0,
106 RTD2132_SSCG_DISABLE);
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700107
108 /* SSCG_Config_1 */
Aaron Durbinebad1762013-08-14 11:27:40 -0500109 rtd2132_write_reg(dev, RTD2132_COMMAND_SSCG_CONFIG_1,
110 RTD2132_SSCG_CONFIG_DISABLED);
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700111}
112
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100113static void rtd2132_sscg(struct device *dev, struct drivers_i2c_rtd2132_config *cfg)
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700114{
Aaron Durbinebad1762013-08-14 11:27:40 -0500115 switch (cfg->sscg_percent) {
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700116 case RTD2132_SSCG_PERCENT_0_0:
117 printk(BIOS_INFO, "RTD2132: Disable Spread Spectrum\n");
118 rtd2132_sscg_disable(dev);
119 break;
120 case RTD2132_SSCG_PERCENT_0_5:
121 printk(BIOS_INFO, "RTD2132: Enable 0.5%% Spread Spectrum\n");
122 rtd2132_sscg_enable(dev, RTD2132_SSCG_CONFIG_0_5);
123 break;
124 case RTD2132_SSCG_PERCENT_1_0:
125 printk(BIOS_INFO, "RTD2132: Enable 1.0%% Spread Spectrum\n");
126 rtd2132_sscg_enable(dev, RTD2132_SSCG_CONFIG_1_0);
127 break;
128 case RTD2132_SSCG_PERCENT_1_5:
129 printk(BIOS_INFO, "RTD2132: Enable 1.5%% Spread Spectrum\n");
130 rtd2132_sscg_enable(dev, RTD2132_SSCG_CONFIG_1_5);
131 break;
132 default:
133 printk(BIOS_ERR, "RTD2132: Invalid Spread Spectrum 0x%02x\n",
Aaron Durbinebad1762013-08-14 11:27:40 -0500134 cfg->sscg_percent);
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700135 }
Aaron Durbinebad1762013-08-14 11:27:40 -0500136}
137
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100138static void rtd2132_lvds_swap(struct device *dev,
Martin Rothb9810a42017-07-23 20:00:04 -0600139 struct drivers_i2c_rtd2132_config *cfg)
Aaron Durbinebad1762013-08-14 11:27:40 -0500140{
141 u8 swap_value = RTD2132_LVDS_SWAP_NORMAL;
142
143 switch (cfg->lvds_swap & ~RTD2132_LVDS_SWAP_CFG_DUAL) {
144 case RTD2132_LVDS_SWAP_CFG_NORMAL:
145 swap_value = RTD2132_LVDS_SWAP_NORMAL;
146 break;
147 case RTD2132_LVDS_SWAP_CFG_MIRROR:
148 swap_value = RTD2132_LVDS_SWAP_MIRROR;
149 break;
150 case RTD2132_LVDS_SWAP_CFG_P_N:
151 swap_value = RTD2132_LVDS_SWAP_P_N;
152 break;
153 case RTD2132_LVDS_SWAP_CFG_MIRROR_P_N:
154 swap_value = RTD2132_LVDS_SWAP_MIRROR_P_N;
155 break;
156 case RTD2132_LVDS_SWAP_CFG_R_L:
157 swap_value = RTD2132_LVDS_SWAP_R_L;
158 break;
159 default:
160 printk(BIOS_ERR, "RTD2132: Invalid LVDS swap value 0x%02x\n",
161 cfg->lvds_swap);
162 }
163
164 if (cfg->lvds_swap & RTD2132_LVDS_SWAP_CFG_DUAL)
165 swap_value |= RTD2132_LVDS_SWAP_DUAL;
166
Idwer Volleringd26da9c2013-12-22 21:38:18 +0000167 printk(BIOS_INFO, "RTD2132: LVDS Swap 0x%02x\n", swap_value);
Aaron Durbinebad1762013-08-14 11:27:40 -0500168
169 rtd2132_write_reg(dev, RTD2132_COMMAND_LVDS_SWAP, swap_value);
170}
171
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100172static void rtd2132_defaults(struct device *dev)
Aaron Durbinebad1762013-08-14 11:27:40 -0500173{
174 static const struct def_setting {
175 u8 reg;
176 u8 value;
177 } def_settings[] = {
178 { 0x3c, 0x06 },
179 { 0x3d, 0x38 },
180 { 0x3e, 0x73 },
181 { 0x3f, 0x33 },
182 { 0x06, 0x90 },
183 { 0x06, 0xb0 },
184 { 0x06, 0x80 },
185 };
186 int i;
187
188 for (i = 0; i < ARRAY_SIZE(def_settings); i++)
189 rtd2132_write_reg(dev, def_settings[i].reg,
190 def_settings[i].value);
191}
192
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100193static void rtd2132_setup(struct device *dev)
Aaron Durbinebad1762013-08-14 11:27:40 -0500194{
195 struct drivers_i2c_rtd2132_config *config = dev->chip_info;
196
197 if (!config)
198 return;
199
200 /* Stop running firmware */
201 rtd2132_firmware_stop(dev);
202
203 /* Panel Power Sequencing Settings. */
204 rtd2132_pps(dev, config);
205
206 /* Spread spectrum configuration */
207 rtd2132_sscg(dev, config);
208
209 /* LVDS Swap Setting. */
210 rtd2132_lvds_swap(dev, config);
211
212 /* Default settings. */
213 rtd2132_defaults(dev);
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700214
215 /* Start firmware */
216 rtd2132_firmware_start(dev);
217}
218
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100219static void rtd2132_init(struct device *dev)
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700220{
221 if (dev->enabled && dev->path.type == DEVICE_PATH_I2C &&
222 ops_smbus_bus(get_pbus_smbus(dev))) {
223 rtd2132_setup(dev);
224 }
225}
226
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700227static struct device_operations rtd2132_operations = {
Edward O'Callaghan524625d2014-10-31 07:55:45 +1100228 .read_resources = DEVICE_NOOP,
229 .set_resources = DEVICE_NOOP,
230 .enable_resources = DEVICE_NOOP,
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700231 .init = rtd2132_init,
232};
233
234static void enable_dev(struct device *dev)
235{
236 dev->ops = &rtd2132_operations;
237}
238
239struct chip_operations drivers_i2c_rtd2132_ops = {
240 CHIP_NAME("Realtek RTD2132 LVDS Bridge")
241 .enable_dev = enable_dev,
242};