1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) Guangzhou FriendlyARM Computer Tech. Co., Ltd.
4 * (http://www.friendlyarm.com)
11 #include <asm/arch/clk.h>
12 #include <asm/arch/pwm.h>
17 #include <asm/arch/nexell.h>
18 #include <asm/arch/nx_gpio.h>
21 #define NSEC_PER_SEC 1000000000L
24 #define SAMPLE_BPS 9600
25 #define SAMPLE_IN_US 101 /* (1000000 / BPS) */
27 #define REQ_INFO 0x60U
31 #define ONEWIRE_I2C_BUS 2
32 #define ONEWIRE_I2C_ADDR 0x2f
34 static int bus_type = -1;
35 static int lcd_id = -1;
36 static unsigned short lcd_fwrev;
37 static int current_brightness = -1;
38 #if CONFIG_IS_ENABLED(DM_I2C)
39 static struct udevice *i2c_dev;
44 #define DBGOUT(msg...) do { printf("onewire: " msg); } while (0)
46 #define DBGOUT(msg...) do {} while (0)
49 /* based on web page from http://lfh1986.blogspot.com */
50 static unsigned char crc8_ow(unsigned int v, unsigned int len)
52 unsigned char crc = 0xACU;
55 if ((crc & 0x80U) != 0) {
61 if ((v & (1U << 31)) != 0)
69 #define __IO_GRP 2 /* GPIOC15 */
72 static inline void set_pin_as_input(void)
74 nx_gpio_set_output_enable(__IO_GRP, __IO_IDX, 0);
77 static inline void set_pin_as_output(void)
79 nx_gpio_set_output_enable(__IO_GRP, __IO_IDX, 1);
82 static inline void set_pin_value(int v)
84 nx_gpio_set_output_value(__IO_GRP, __IO_IDX, !!v);
87 static inline int get_pin_value(void)
89 return nx_gpio_get_input_value(__IO_GRP, __IO_IDX);
94 #define PWM_TCON (PHY_BASEADDR_PWM + 0x08)
95 #define PWM_TCON_START (1 << 16)
96 #define PWM_TINT_CSTAT (PHY_BASEADDR_PWM + 0x44)
98 static int onewire_init_timer(void)
100 int period_ns = NSEC_PER_SEC / SAMPLE_BPS;
102 /* range: 1080~1970 */
105 return s5p_pwm_config(PWM_CH, period_ns >> 1, period_ns);
108 static void wait_one_tick(void)
112 tcon = readl(PWM_TCON);
113 tcon |= PWM_TCON_START;
114 writel(tcon, PWM_TCON);
117 if (readl(PWM_TINT_CSTAT) & (1 << (5 + PWM_CH)))
121 writel((1 << (5 + PWM_CH)), PWM_TINT_CSTAT);
123 tcon &= ~PWM_TCON_START;
124 writel(tcon, PWM_TCON);
127 /* Session handler */
128 static int onewire_session(unsigned char req, unsigned char res[])
132 int ints = disable_interrupts();
136 Req = (req << 24) | (crc8_ow(req << 24, 8) << 16);
137 Res = (unsigned int *)res;
141 for (i = 0; i < 60; i++)
145 for (i = 0; i < 2; i++)
148 for (i = 0; i < 16; i++) {
149 int v = !!(Req & (1U << 31));
159 for (i = 0; i < 32; i++) {
161 (*Res) |= get_pin_value();
170 ret = crc8_ow(*Res, 24) == res[0];
171 DBGOUT("req = %02X, res = %02X%02X%02X%02X, ret = %d\n",
172 req, res[3], res[2], res[1], res[0], ret);
177 static int onewire_i2c_do_request(unsigned char req, unsigned char *buf)
183 tx[1] = crc8_ow(req << 24, 8);
185 #if CONFIG_IS_ENABLED(DM_I2C)
186 if (dm_i2c_write(i2c_dev, 0, tx, 2))
192 if (dm_i2c_read(i2c_dev, 0, buf, 4))
195 if (i2c_write(ONEWIRE_I2C_ADDR, 0, 0, tx, 2))
198 if (!buf) /* NO READ */
201 if (i2c_read(ONEWIRE_I2C_ADDR, 0, 0, buf, 4))
205 ret = crc8_ow((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8), 24);
206 DBGOUT("req = %02X, res = %02X%02X%02X%02X, ret = %02x\n",
207 req, buf[0], buf[1], buf[2], buf[3], ret);
209 return (ret == buf[3]) ? 0 : -EIO;
212 static void onewire_i2c_init(void)
214 unsigned char buf[4];
217 #if CONFIG_IS_ENABLED(DM_I2C)
218 ret = i2c_get_chip_for_busnum(ONEWIRE_I2C_BUS,
219 ONEWIRE_I2C_ADDR, 0, &i2c_dev);
221 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
222 i2c_set_bus_num(ONEWIRE_I2C_BUS);
224 ret = i2c_probe(ONEWIRE_I2C_ADDR);
229 ret = onewire_i2c_do_request(REQ_INFO, buf);
232 lcd_fwrev = buf[1] * 0x100 + buf[2];
237 void onewire_init(void)
240 nx_gpio_set_pad_function(__IO_GRP, __IO_IDX, 1);
241 nx_gpio_set_pull_mode(__IO_GRP, __IO_IDX, 2);
243 onewire_init_timer();
247 int onewire_get_info(unsigned char *lcd, unsigned short *fw_ver)
249 unsigned char res[4];
252 if (bus_type == BUS_I2C && lcd_id > 0) {
258 for (i = 0; i < 3; i++) {
259 if (onewire_session(REQ_INFO, res)) {
261 *fw_ver = res[2] * 0x100 + res[1];
263 DBGOUT("lcd = %d, fw_ver = %x\n", *lcd, *fw_ver);
268 /* LCD unknown or not connected */
275 int onewire_get_lcd_id(void)
280 int onewire_set_backlight(int brightness)
282 unsigned char res[4];
285 if (brightness == current_brightness)
288 if (brightness > 127)
290 else if (brightness < 0)
293 if (bus_type == BUS_I2C) {
294 onewire_i2c_do_request((REQ_BL | brightness), NULL);
295 current_brightness = brightness;
299 for (i = 0; i < 3; i++) {
300 if (onewire_session((REQ_BL | brightness), res)) {
301 current_brightness = brightness;