xref: /linux/drivers/video/backlight/ili9320.c (revision 2fb7eb3d7e8c5c0375c726c7d5c443e6f7e53741)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* drivers/video/backlight/ili9320.c
3  *
4  * ILI9320 LCD controller driver core.
5  *
6  * Copyright 2007 Simtec Electronics
7  *	http://armlinux.simtec.co.uk/
8  *	Ben Dooks <ben@simtec.co.uk>
9 */
10 
11 #include <linux/delay.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/lcd.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 
18 #include <linux/spi/spi.h>
19 
20 #include <video/ili9320.h>
21 
22 #include "ili9320.h"
23 
24 
ili9320_write_spi(struct ili9320 * ili,unsigned int reg,unsigned int value)25 static inline int ili9320_write_spi(struct ili9320 *ili,
26 				    unsigned int reg,
27 				    unsigned int value)
28 {
29 	struct ili9320_spi *spi = &ili->access.spi;
30 	unsigned char *addr = spi->buffer_addr;
31 	unsigned char *data = spi->buffer_data;
32 
33 	/* spi message consits of:
34 	 * first byte: ID and operation
35 	 */
36 
37 	addr[0] = spi->id | ILI9320_SPI_INDEX | ILI9320_SPI_WRITE;
38 	addr[1] = reg >> 8;
39 	addr[2] = reg;
40 
41 	/* second message is the data to transfer */
42 
43 	data[0] = spi->id | ILI9320_SPI_DATA  | ILI9320_SPI_WRITE;
44 	data[1] = value >> 8;
45 	data[2] = value;
46 
47 	return spi_sync(spi->dev, &spi->message);
48 }
49 
ili9320_write(struct ili9320 * ili,unsigned int reg,unsigned int value)50 int ili9320_write(struct ili9320 *ili, unsigned int reg, unsigned int value)
51 {
52 	dev_dbg(ili->dev, "write: reg=%02x, val=%04x\n", reg, value);
53 	return ili->write(ili, reg, value);
54 }
55 EXPORT_SYMBOL_GPL(ili9320_write);
56 
ili9320_write_regs(struct ili9320 * ili,const struct ili9320_reg * values,int nr_values)57 int ili9320_write_regs(struct ili9320 *ili,
58 		       const struct ili9320_reg *values,
59 		       int nr_values)
60 {
61 	int index;
62 	int ret;
63 
64 	for (index = 0; index < nr_values; index++, values++) {
65 		ret = ili9320_write(ili, values->address, values->value);
66 		if (ret != 0)
67 			return ret;
68 	}
69 
70 	return 0;
71 }
72 EXPORT_SYMBOL_GPL(ili9320_write_regs);
73 
ili9320_reset(struct ili9320 * lcd)74 static void ili9320_reset(struct ili9320 *lcd)
75 {
76 	struct ili9320_platdata *cfg = lcd->platdata;
77 
78 	cfg->reset(1);
79 	mdelay(50);
80 
81 	cfg->reset(0);
82 	mdelay(50);
83 
84 	cfg->reset(1);
85 	mdelay(100);
86 }
87 
ili9320_init_chip(struct ili9320 * lcd)88 static inline int ili9320_init_chip(struct ili9320 *lcd)
89 {
90 	int ret;
91 
92 	ili9320_reset(lcd);
93 
94 	ret = lcd->client->init(lcd, lcd->platdata);
95 	if (ret != 0) {
96 		dev_err(lcd->dev, "failed to initialise display\n");
97 		return ret;
98 	}
99 
100 	lcd->initialised = 1;
101 	return 0;
102 }
103 
ili9320_power_on(struct ili9320 * lcd)104 static inline int ili9320_power_on(struct ili9320 *lcd)
105 {
106 	if (!lcd->initialised)
107 		ili9320_init_chip(lcd);
108 
109 	lcd->display1 |= (ILI9320_DISPLAY1_D(3) | ILI9320_DISPLAY1_BASEE);
110 	ili9320_write(lcd, ILI9320_DISPLAY1, lcd->display1);
111 
112 	return 0;
113 }
114 
ili9320_power_off(struct ili9320 * lcd)115 static inline int ili9320_power_off(struct ili9320 *lcd)
116 {
117 	lcd->display1 &= ~(ILI9320_DISPLAY1_D(3) | ILI9320_DISPLAY1_BASEE);
118 	ili9320_write(lcd, ILI9320_DISPLAY1, lcd->display1);
119 
120 	return 0;
121 }
122 
123 #define POWER_IS_ON(pwr)	((pwr) <= LCD_POWER_REDUCED)
124 
ili9320_power(struct ili9320 * lcd,int power)125 static int ili9320_power(struct ili9320 *lcd, int power)
126 {
127 	int ret = 0;
128 
129 	dev_dbg(lcd->dev, "power %d => %d\n", lcd->power, power);
130 
131 	if (POWER_IS_ON(power) && !POWER_IS_ON(lcd->power))
132 		ret = ili9320_power_on(lcd);
133 	else if (!POWER_IS_ON(power) && POWER_IS_ON(lcd->power))
134 		ret = ili9320_power_off(lcd);
135 
136 	if (ret == 0)
137 		lcd->power = power;
138 	else
139 		dev_warn(lcd->dev, "failed to set power mode %d\n", power);
140 
141 	return ret;
142 }
143 
to_our_lcd(struct lcd_device * lcd)144 static inline struct ili9320 *to_our_lcd(struct lcd_device *lcd)
145 {
146 	return lcd_get_data(lcd);
147 }
148 
ili9320_set_power(struct lcd_device * ld,int power)149 static int ili9320_set_power(struct lcd_device *ld, int power)
150 {
151 	struct ili9320 *lcd = to_our_lcd(ld);
152 
153 	return ili9320_power(lcd, power);
154 }
155 
ili9320_get_power(struct lcd_device * ld)156 static int ili9320_get_power(struct lcd_device *ld)
157 {
158 	struct ili9320 *lcd = to_our_lcd(ld);
159 
160 	return lcd->power;
161 }
162 
163 static const struct lcd_ops ili9320_ops = {
164 	.get_power	= ili9320_get_power,
165 	.set_power	= ili9320_set_power,
166 };
167 
ili9320_setup_spi(struct ili9320 * ili,struct spi_device * dev)168 static void ili9320_setup_spi(struct ili9320 *ili,
169 					struct spi_device *dev)
170 {
171 	struct ili9320_spi *spi = &ili->access.spi;
172 
173 	ili->write = ili9320_write_spi;
174 	spi->dev = dev;
175 
176 	/* fill the two messages we are going to use to send the data
177 	 * with, the first the address followed by the data. The datasheet
178 	 * says they should be done as two distinct cycles of the SPI CS line.
179 	 */
180 
181 	spi->xfer[0].tx_buf = spi->buffer_addr;
182 	spi->xfer[1].tx_buf = spi->buffer_data;
183 	spi->xfer[0].len = 3;
184 	spi->xfer[1].len = 3;
185 	spi->xfer[0].bits_per_word = 8;
186 	spi->xfer[1].bits_per_word = 8;
187 	spi->xfer[0].cs_change = 1;
188 
189 	spi_message_init(&spi->message);
190 	spi_message_add_tail(&spi->xfer[0], &spi->message);
191 	spi_message_add_tail(&spi->xfer[1], &spi->message);
192 }
193 
ili9320_probe_spi(struct spi_device * spi,struct ili9320_client * client)194 int ili9320_probe_spi(struct spi_device *spi,
195 				struct ili9320_client *client)
196 {
197 	struct ili9320_platdata *cfg = dev_get_platdata(&spi->dev);
198 	struct device *dev = &spi->dev;
199 	struct ili9320 *ili;
200 	struct lcd_device *lcd;
201 	int ret = 0;
202 
203 	/* verify we where given some information */
204 
205 	if (cfg == NULL) {
206 		dev_err(dev, "no platform data supplied\n");
207 		return -EINVAL;
208 	}
209 
210 	if (cfg->hsize <= 0 || cfg->vsize <= 0 || cfg->reset == NULL) {
211 		dev_err(dev, "invalid platform data supplied\n");
212 		return -EINVAL;
213 	}
214 
215 	/* allocate and initialse our state */
216 
217 	ili = devm_kzalloc(&spi->dev, sizeof(struct ili9320), GFP_KERNEL);
218 	if (ili == NULL)
219 		return -ENOMEM;
220 
221 	ili->access.spi.id = ILI9320_SPI_IDCODE | ILI9320_SPI_ID(1);
222 
223 	ili->dev = dev;
224 	ili->client = client;
225 	ili->power = LCD_POWER_OFF;
226 	ili->platdata = cfg;
227 
228 	spi_set_drvdata(spi, ili);
229 
230 	ili9320_setup_spi(ili, spi);
231 
232 	lcd = devm_lcd_device_register(&spi->dev, "ili9320", dev, ili,
233 					&ili9320_ops);
234 	if (IS_ERR(lcd)) {
235 		dev_err(dev, "failed to register lcd device\n");
236 		return PTR_ERR(lcd);
237 	}
238 
239 	ili->lcd = lcd;
240 
241 	dev_info(dev, "initialising %s\n", client->name);
242 
243 	ret = ili9320_power(ili, LCD_POWER_ON);
244 	if (ret != 0) {
245 		dev_err(dev, "failed to set lcd power state\n");
246 		return ret;
247 	}
248 
249 	return 0;
250 }
251 EXPORT_SYMBOL_GPL(ili9320_probe_spi);
252 
ili9320_remove(struct ili9320 * ili)253 void ili9320_remove(struct ili9320 *ili)
254 {
255 	ili9320_power(ili, LCD_POWER_OFF);
256 }
257 EXPORT_SYMBOL_GPL(ili9320_remove);
258 
259 #ifdef CONFIG_PM_SLEEP
ili9320_suspend(struct ili9320 * lcd)260 int ili9320_suspend(struct ili9320 *lcd)
261 {
262 	int ret;
263 
264 	ret = ili9320_power(lcd, LCD_POWER_OFF);
265 
266 	if (lcd->platdata->suspend == ILI9320_SUSPEND_DEEP) {
267 		ili9320_write(lcd, ILI9320_POWER1, lcd->power1 |
268 			      ILI9320_POWER1_SLP |
269 			      ILI9320_POWER1_DSTB);
270 		lcd->initialised = 0;
271 	}
272 
273 	return ret;
274 }
275 EXPORT_SYMBOL_GPL(ili9320_suspend);
276 
ili9320_resume(struct ili9320 * lcd)277 int ili9320_resume(struct ili9320 *lcd)
278 {
279 	dev_info(lcd->dev, "resuming from power state %d\n", lcd->power);
280 
281 	if (lcd->platdata->suspend == ILI9320_SUSPEND_DEEP)
282 		ili9320_write(lcd, ILI9320_POWER1, 0x00);
283 
284 	return ili9320_power(lcd, LCD_POWER_ON);
285 }
286 EXPORT_SYMBOL_GPL(ili9320_resume);
287 #endif
288 
289 /* Power down all displays on reboot, poweroff or halt */
ili9320_shutdown(struct ili9320 * lcd)290 void ili9320_shutdown(struct ili9320 *lcd)
291 {
292 	ili9320_power(lcd, LCD_POWER_OFF);
293 }
294 EXPORT_SYMBOL_GPL(ili9320_shutdown);
295 
296 MODULE_AUTHOR("Ben Dooks <ben-linux@fluff.org>");
297 MODULE_DESCRIPTION("ILI9320 LCD Driver");
298 MODULE_LICENSE("GPL v2");
299