blob: 2aaa234d78e7def1713b927653079c003397e188 [file] [log] [blame]
Duncan Laurie0cf0d142013-07-15 09:11:21 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2013 Google Inc. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Duncan Laurie0cf0d142013-07-15 09:11:21 -070014 */
15
Aaron Durbinebad1762013-08-14 11:27:40 -050016#include <stdlib.h>
Duncan Laurie0cf0d142013-07-15 09:11:21 -070017#include <console/console.h>
18#include <delay.h>
19#include <device/device.h>
20#include <device/smbus.h>
21#include <device/pci.h>
22#include "chip.h"
23
24/* Chip commands */
25#define RTD2132_COMMAND 0x01
26#define RTD2132_DATA 0x00
27#define RTD2132_FIRMWARE 0x80
28#define RTD2132_FIRMWARE_START 0x00
29#define RTD2132_FIRMWARE_STOP 0x01
30
Aaron Durbinebad1762013-08-14 11:27:40 -050031/* Panel Power Sequence Timing Registers. */
32#define RTD2132_COMMAND_PWR_SEQ_T1 0x32 /* 1ms units. */
33#define RTD2132_COMMAND_PWR_SEQ_T2 0x33 /* 4ms units. */
34#define RTD2132_COMMAND_PWR_SEQ_T3 0x34 /* 1ms units. */
35#define RTD2132_COMMAND_PWR_SEQ_T4 0x35 /* 1ms units. */
36#define RTD2132_COMMAND_PWR_SEQ_T5 0x36 /* 4ms units. */
37#define RTD2132_COMMAND_PWR_SEQ_T6 0x37 /* 1ms units. */
38#define RTD2132_COMMAND_PWR_SEQ_T7 0x38 /* 4ms units. */
39
Duncan Laurie0cf0d142013-07-15 09:11:21 -070040/* Spread spectrum configuration */
41#define RTD2132_COMMAND_SSCG_CONFIG_0 0x39
42#define RTD2132_SSCG_ENABLE 0xa0
43#define RTD2132_SSCG_DISABLE 0x20
44#define RTD2132_COMMAND_SSCG_CONFIG_1 0x3a
45#define RTD2132_SSCG_CONFIG_DISABLED 0x01 /* DISABLED */
46#define RTD2132_SSCG_CONFIG_0_5 0x07 /* 0.5% */
47#define RTD2132_SSCG_CONFIG_1_0 0x0f /* 1.0% */
48#define RTD2132_SSCG_CONFIG_1_5 0x16 /* 1.5% */
49
Aaron Durbinebad1762013-08-14 11:27:40 -050050/* LVDS Swap */
51#define RTD2132_COMMAND_LVDS_SWAP 0x3b
52#define RTD2132_LVDS_SWAP_DUAL 0x80
53#define RTD2132_LVDS_SWAP_NORMAL 0x04
54#define RTD2132_LVDS_SWAP_MIRROR 0x14
55#define RTD2132_LVDS_SWAP_P_N 0x24
56#define RTD2132_LVDS_SWAP_MIRROR_P_N 0x34
57#define RTD2132_LVDS_SWAP_R_L 0x0c
58
Duncan Laurie0cf0d142013-07-15 09:11:21 -070059/* Configuration values from devicetree */
60#define RTD2132_SSCG_PERCENT_0_0 0x00 /* DISABLED */
61#define RTD2132_SSCG_PERCENT_0_5 0x05 /* 0.5% */
62#define RTD2132_SSCG_PERCENT_1_0 0x10 /* 1.0% */
63#define RTD2132_SSCG_PERCENT_1_5 0x15 /* 1.5% */
64
Aaron Durbinebad1762013-08-14 11:27:40 -050065#define RTD2132_LVDS_SWAP_CFG_DUAL 0x80
66#define RTD2132_LVDS_SWAP_CFG_NORMAL 0x00
67#define RTD2132_LVDS_SWAP_CFG_MIRROR 0x01
68#define RTD2132_LVDS_SWAP_CFG_P_N 0x02
69#define RTD2132_LVDS_SWAP_CFG_MIRROR_P_N 0x03
70#define RTD2132_LVDS_SWAP_CFG_R_L 0x04
71
72#define RTD2132_DEBUG_REG 0
73
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110074static void rtd2132_write_reg(struct device *dev, u8 reg, u8 value)
Aaron Durbinebad1762013-08-14 11:27:40 -050075{
76 if (RTD2132_DEBUG_REG)
77 printk(BIOS_DEBUG, "RTD2132 0x%02x <- 0x%02x\n", reg, value);
78 smbus_write_byte(dev, RTD2132_COMMAND, reg);
79 smbus_write_byte(dev, RTD2132_DATA, value);
80}
81
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110082static void rtd2132_firmware_stop(struct device *dev)
Duncan Laurie0cf0d142013-07-15 09:11:21 -070083{
84 smbus_write_byte(dev, RTD2132_FIRMWARE, RTD2132_FIRMWARE_STOP);
Aaron Durbinebad1762013-08-14 11:27:40 -050085 mdelay(60);
Duncan Laurie0cf0d142013-07-15 09:11:21 -070086}
87
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110088static void rtd2132_firmware_start(struct device *dev)
Duncan Laurie0cf0d142013-07-15 09:11:21 -070089{
90 smbus_write_byte(dev, RTD2132_FIRMWARE, RTD2132_FIRMWARE_START);
91}
92
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110093static void rtd2132_pps(struct device *dev, struct drivers_i2c_rtd2132_config *cfg)
Aaron Durbinebad1762013-08-14 11:27:40 -050094{
95 /* T2, T5, and T7 register values are in units of 4ms. */
96 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T1, cfg->t1);
97 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T2, cfg->t2 / 4);
98 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T3, cfg->t3);
99 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T4, cfg->t4);
100 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T5, cfg->t5 / 4);
101 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T6, cfg->t6);
102 rtd2132_write_reg(dev, RTD2132_COMMAND_PWR_SEQ_T7, cfg->t7 / 4);
103}
104
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100105static void rtd2132_sscg_enable(struct device *dev, u8 sscg_percent)
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700106{
107 /* SSCG_Config_0 */
Aaron Durbinebad1762013-08-14 11:27:40 -0500108 rtd2132_write_reg(dev, RTD2132_COMMAND_SSCG_CONFIG_0,
109 RTD2132_SSCG_ENABLE);
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700110
111 /* SSCG_Config_1 */
Aaron Durbinebad1762013-08-14 11:27:40 -0500112 rtd2132_write_reg(dev, RTD2132_COMMAND_SSCG_CONFIG_1, sscg_percent);
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700113}
114
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100115static void rtd2132_sscg_disable(struct device *dev)
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700116{
117 /* SSCG_Config_0 */
Aaron Durbinebad1762013-08-14 11:27:40 -0500118 rtd2132_write_reg(dev, RTD2132_COMMAND_SSCG_CONFIG_0,
119 RTD2132_SSCG_DISABLE);
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700120
121 /* SSCG_Config_1 */
Aaron Durbinebad1762013-08-14 11:27:40 -0500122 rtd2132_write_reg(dev, RTD2132_COMMAND_SSCG_CONFIG_1,
123 RTD2132_SSCG_CONFIG_DISABLED);
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700124}
125
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100126static void rtd2132_sscg(struct device *dev, struct drivers_i2c_rtd2132_config *cfg)
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700127{
Aaron Durbinebad1762013-08-14 11:27:40 -0500128 switch (cfg->sscg_percent) {
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700129 case RTD2132_SSCG_PERCENT_0_0:
130 printk(BIOS_INFO, "RTD2132: Disable Spread Spectrum\n");
131 rtd2132_sscg_disable(dev);
132 break;
133 case RTD2132_SSCG_PERCENT_0_5:
134 printk(BIOS_INFO, "RTD2132: Enable 0.5%% Spread Spectrum\n");
135 rtd2132_sscg_enable(dev, RTD2132_SSCG_CONFIG_0_5);
136 break;
137 case RTD2132_SSCG_PERCENT_1_0:
138 printk(BIOS_INFO, "RTD2132: Enable 1.0%% Spread Spectrum\n");
139 rtd2132_sscg_enable(dev, RTD2132_SSCG_CONFIG_1_0);
140 break;
141 case RTD2132_SSCG_PERCENT_1_5:
142 printk(BIOS_INFO, "RTD2132: Enable 1.5%% Spread Spectrum\n");
143 rtd2132_sscg_enable(dev, RTD2132_SSCG_CONFIG_1_5);
144 break;
145 default:
146 printk(BIOS_ERR, "RTD2132: Invalid Spread Spectrum 0x%02x\n",
Aaron Durbinebad1762013-08-14 11:27:40 -0500147 cfg->sscg_percent);
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700148 }
Aaron Durbinebad1762013-08-14 11:27:40 -0500149}
150
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100151static void rtd2132_lvds_swap(struct device *dev,
Martin Rothb9810a42017-07-23 20:00:04 -0600152 struct drivers_i2c_rtd2132_config *cfg)
Aaron Durbinebad1762013-08-14 11:27:40 -0500153{
154 u8 swap_value = RTD2132_LVDS_SWAP_NORMAL;
155
156 switch (cfg->lvds_swap & ~RTD2132_LVDS_SWAP_CFG_DUAL) {
157 case RTD2132_LVDS_SWAP_CFG_NORMAL:
158 swap_value = RTD2132_LVDS_SWAP_NORMAL;
159 break;
160 case RTD2132_LVDS_SWAP_CFG_MIRROR:
161 swap_value = RTD2132_LVDS_SWAP_MIRROR;
162 break;
163 case RTD2132_LVDS_SWAP_CFG_P_N:
164 swap_value = RTD2132_LVDS_SWAP_P_N;
165 break;
166 case RTD2132_LVDS_SWAP_CFG_MIRROR_P_N:
167 swap_value = RTD2132_LVDS_SWAP_MIRROR_P_N;
168 break;
169 case RTD2132_LVDS_SWAP_CFG_R_L:
170 swap_value = RTD2132_LVDS_SWAP_R_L;
171 break;
172 default:
173 printk(BIOS_ERR, "RTD2132: Invalid LVDS swap value 0x%02x\n",
174 cfg->lvds_swap);
175 }
176
177 if (cfg->lvds_swap & RTD2132_LVDS_SWAP_CFG_DUAL)
178 swap_value |= RTD2132_LVDS_SWAP_DUAL;
179
Idwer Volleringd26da9c2013-12-22 21:38:18 +0000180 printk(BIOS_INFO, "RTD2132: LVDS Swap 0x%02x\n", swap_value);
Aaron Durbinebad1762013-08-14 11:27:40 -0500181
182 rtd2132_write_reg(dev, RTD2132_COMMAND_LVDS_SWAP, swap_value);
183}
184
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100185static void rtd2132_defaults(struct device *dev)
Aaron Durbinebad1762013-08-14 11:27:40 -0500186{
187 static const struct def_setting {
188 u8 reg;
189 u8 value;
190 } def_settings[] = {
191 { 0x3c, 0x06 },
192 { 0x3d, 0x38 },
193 { 0x3e, 0x73 },
194 { 0x3f, 0x33 },
195 { 0x06, 0x90 },
196 { 0x06, 0xb0 },
197 { 0x06, 0x80 },
198 };
199 int i;
200
201 for (i = 0; i < ARRAY_SIZE(def_settings); i++)
202 rtd2132_write_reg(dev, def_settings[i].reg,
203 def_settings[i].value);
204}
205
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100206static void rtd2132_setup(struct device *dev)
Aaron Durbinebad1762013-08-14 11:27:40 -0500207{
208 struct drivers_i2c_rtd2132_config *config = dev->chip_info;
209
210 if (!config)
211 return;
212
213 /* Stop running firmware */
214 rtd2132_firmware_stop(dev);
215
216 /* Panel Power Sequencing Settings. */
217 rtd2132_pps(dev, config);
218
219 /* Spread spectrum configuration */
220 rtd2132_sscg(dev, config);
221
222 /* LVDS Swap Setting. */
223 rtd2132_lvds_swap(dev, config);
224
225 /* Default settings. */
226 rtd2132_defaults(dev);
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700227
228 /* Start firmware */
229 rtd2132_firmware_start(dev);
230}
231
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100232static void rtd2132_init(struct device *dev)
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700233{
234 if (dev->enabled && dev->path.type == DEVICE_PATH_I2C &&
235 ops_smbus_bus(get_pbus_smbus(dev))) {
236 rtd2132_setup(dev);
237 }
238}
239
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700240static struct device_operations rtd2132_operations = {
Edward O'Callaghan524625d2014-10-31 07:55:45 +1100241 .read_resources = DEVICE_NOOP,
242 .set_resources = DEVICE_NOOP,
243 .enable_resources = DEVICE_NOOP,
Duncan Laurie0cf0d142013-07-15 09:11:21 -0700244 .init = rtd2132_init,
245};
246
247static void enable_dev(struct device *dev)
248{
249 dev->ops = &rtd2132_operations;
250}
251
252struct chip_operations drivers_i2c_rtd2132_ops = {
253 CHIP_NAME("Realtek RTD2132 LVDS Bridge")
254 .enable_dev = enable_dev,
255};