MC13783 cleanup. Remove functions proving themselves not so useful and add mc13783_wr...
[kugel-rb.git] / firmware / target / arm / imx31 / gigabeat-s / mc13783-imx31.c
blobc185994bfc17a5db23296863859990c004a6e430
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (c) 2008 by Michael Sevakis
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "system.h"
22 #include "cpu.h"
23 #include "spi-imx31.h"
24 #include "gpio-imx31.h"
25 #include "mc13783.h"
26 #include "debug.h"
27 #include "kernel.h"
29 #include "power-imx31.h"
30 #include "button-target.h"
31 #include "adc-target.h"
32 #include "usb-target.h"
34 #ifdef BOOTLOADER
35 #define PMIC_DRIVER_CLOSE
36 #endif
38 /* This is all based on communicating with the MC13783 PMU which is on
39 * CSPI2 with the chip select at 0. The LCD controller resides on
40 * CSPI3 cs1, but we have no idea how to communicate to it */
41 static struct spi_node mc13783_spi =
43 CSPI2_NUM, /* CSPI module 2 */
44 CSPI_CONREG_CHIP_SELECT_SS0 | /* Chip select 0 */
45 CSPI_CONREG_DRCTL_DONT_CARE | /* Don't care about CSPI_RDY */
46 CSPI_CONREG_DATA_RATE_DIV_4 | /* Clock = IPG_CLK/4 - 16.5MHz */
47 CSPI_BITCOUNT(32-1) | /* All 32 bits are to be transferred */
48 CSPI_CONREG_SSPOL | /* SS active high */
49 CSPI_CONREG_SSCTL | /* Negate SS between SPI bursts */
50 CSPI_CONREG_MODE, /* Master mode */
51 0, /* SPI clock - no wait states */
54 extern const struct mc13783_event_list mc13783_event_list;
56 static int mc13783_thread_stack[DEFAULT_STACK_SIZE/sizeof(int)];
57 static const char *mc13783_thread_name = "pmic";
58 static struct wakeup mc13783_wake;
60 /* Tracking for which interrupts are enabled */
61 static uint32_t pmic_int_enabled[2] =
62 { 0x00000000, 0x00000000 };
64 static const unsigned char pmic_intm_regs[2] =
65 { MC13783_INTERRUPT_MASK0, MC13783_INTERRUPT_MASK1 };
67 static const unsigned char pmic_ints_regs[2] =
68 { MC13783_INTERRUPT_STATUS0, MC13783_INTERRUPT_STATUS1 };
70 #ifdef PMIC_DRIVER_CLOSE
71 static bool pmic_close = false;
72 static struct thread_entry *mc13783_thread_p = NULL;
73 #endif
75 static void mc13783_interrupt_thread(void)
77 uint32_t pending[2];
79 /* Enable mc13783 GPIO event */
80 gpio_enable_event(MC13783_EVENT_ID);
82 while (1)
84 const struct mc13783_event *event, *event_last;
86 wakeup_wait(&mc13783_wake, TIMEOUT_BLOCK);
88 #ifdef PMIC_DRIVER_CLOSE
89 if (pmic_close)
90 break;
91 #endif
93 mc13783_read_regset(pmic_ints_regs, pending, 2);
95 /* Only clear interrupts being dispatched */
96 pending[0] &= pmic_int_enabled[0];
97 pending[1] &= pmic_int_enabled[1];
99 mc13783_write_regset(pmic_ints_regs, pending, 2);
101 event = mc13783_event_list.events;
102 event_last = event + mc13783_event_list.count;
104 /* .count is surely expected to be > 0 */
107 enum mc13783_event_sets set = event->set;
108 uint32_t pnd = pending[set];
109 uint32_t mask = event->mask;
111 if (pnd & mask)
113 event->callback();
114 pnd &= ~mask;
115 pending[set] = pnd;
118 if ((pending[0] | pending[1]) == 0)
119 break; /* Teminate early if nothing more to service */
121 while (++event < event_last);
124 #ifdef PMIC_DRIVER_CLOSE
125 gpio_disable_event(MC13783_EVENT_ID);
126 #endif
129 /* GPIO interrupt handler for mc13783 */
130 void mc13783_event(void)
132 MC13783_GPIO_ISR = (1ul << MC13783_GPIO_LINE);
133 wakeup_signal(&mc13783_wake);
136 void mc13783_init(void)
138 /* Serial interface must have been initialized first! */
139 wakeup_init(&mc13783_wake);
141 /* Enable the PMIC SPI module */
142 spi_enable_module(&mc13783_spi);
144 /* Mask any PMIC interrupts for now - modules will enable them as
145 * required */
146 mc13783_write(MC13783_INTERRUPT_MASK0, 0xffffff);
147 mc13783_write(MC13783_INTERRUPT_MASK1, 0xffffff);
149 MC13783_GPIO_ISR = (1ul << MC13783_GPIO_LINE);
151 #ifdef PMIC_DRIVER_CLOSE
152 mc13783_thread_p =
153 #endif
154 create_thread(mc13783_interrupt_thread,
155 mc13783_thread_stack, sizeof(mc13783_thread_stack), 0,
156 mc13783_thread_name IF_PRIO(, PRIORITY_REALTIME) IF_COP(, CPU));
159 #ifdef PMIC_DRIVER_CLOSE
160 void mc13783_close(void)
162 struct thread_entry *thread = mc13783_thread_p;
164 if (thread == NULL)
165 return;
167 mc13783_thread_p = NULL;
169 pmic_close = true;
170 wakeup_signal(&mc13783_wake);
171 thread_wait(thread);
173 #endif /* PMIC_DRIVER_CLOSE */
175 bool mc13783_enable_event(enum mc13783_event_ids id)
177 const struct mc13783_event * const event =
178 &mc13783_event_list.events[id];
179 int set = event->set;
180 uint32_t mask = event->mask;
182 spi_lock(&mc13783_spi);
184 pmic_int_enabled[set] |= mask;
185 mc13783_clear(pmic_intm_regs[set], mask);
187 spi_unlock(&mc13783_spi);
189 return true;
192 void mc13783_disable_event(enum mc13783_event_ids id)
194 const struct mc13783_event * const event =
195 &mc13783_event_list.events[id];
196 int set = event->set;
197 uint32_t mask = event->mask;
199 spi_lock(&mc13783_spi);
201 pmic_int_enabled[set] &= ~mask;
202 mc13783_set(pmic_intm_regs[set], mask);
204 spi_unlock(&mc13783_spi);
207 uint32_t mc13783_set(unsigned address, uint32_t bits)
209 spi_lock(&mc13783_spi);
211 uint32_t data = mc13783_read(address);
213 if (data != MC13783_DATA_ERROR)
214 mc13783_write(address, data | bits);
216 spi_unlock(&mc13783_spi);
218 return data;
221 uint32_t mc13783_clear(unsigned address, uint32_t bits)
223 spi_lock(&mc13783_spi);
225 uint32_t data = mc13783_read(address);
227 if (data != MC13783_DATA_ERROR)
228 mc13783_write(address, data & ~bits);
230 spi_unlock(&mc13783_spi);
232 return data;
235 int mc13783_write(unsigned address, uint32_t data)
237 struct spi_transfer xfer;
238 uint32_t packet;
240 if (address >= MC13783_NUM_REGS)
241 return -1;
243 packet = (1 << 31) | (address << 25) | (data & 0xffffff);
244 xfer.txbuf = &packet;
245 xfer.rxbuf = &packet;
246 xfer.count = 1;
248 if (!spi_transfer(&mc13783_spi, &xfer))
249 return -1;
251 return 1 - xfer.count;
254 uint32_t mc13783_write_masked(unsigned address, uint32_t data, uint32_t mask)
256 uint32_t old;
258 spi_lock(&mc13783_spi);
260 old = mc13783_read(address);
262 if (old != MC13783_DATA_ERROR)
264 data = (old & ~mask) | (data & mask);
266 if (mc13783_write(address, data) != 1)
267 old = MC13783_DATA_ERROR;
270 spi_unlock(&mc13783_spi);
272 return old;
275 int mc13783_write_regset(const unsigned char *regs, const uint32_t *data,
276 int count)
278 int i;
279 struct spi_transfer xfer;
280 uint32_t packets[MC13783_NUM_REGS];
282 if ((unsigned)count > MC13783_NUM_REGS)
283 return -1;
285 for (i = 0; i < count; i++)
287 uint32_t reg = regs[i];
289 if (reg >= MC13783_NUM_REGS)
290 return -1;
292 packets[i] = (1 << 31) | (reg << 25) | (data[i] & 0xffffff);
295 xfer.txbuf = packets;
296 xfer.rxbuf = packets;
297 xfer.count = count;
299 if (!spi_transfer(&mc13783_spi, &xfer))
300 return -1;
302 return count - xfer.count;
305 uint32_t mc13783_read(unsigned address)
307 uint32_t packet;
308 struct spi_transfer xfer;
310 if (address >= MC13783_NUM_REGS)
311 return MC13783_DATA_ERROR;
313 packet = address << 25;
315 xfer.txbuf = &packet;
316 xfer.rxbuf = &packet;
317 xfer.count = 1;
319 if (!spi_transfer(&mc13783_spi, &xfer))
320 return MC13783_DATA_ERROR;
322 return packet;
325 int mc13783_read_regset(const unsigned char *regs, uint32_t *buffer,
326 int count)
328 int i;
329 struct spi_transfer xfer;
331 if ((unsigned)count > MC13783_NUM_REGS)
332 return -1;
334 for (i = 0; i < count; i++)
336 unsigned reg = regs[i];
338 if (reg >= MC13783_NUM_REGS)
339 return -1;
341 buffer[i] = reg << 25;
344 xfer.txbuf = buffer;
345 xfer.rxbuf = buffer;
346 xfer.count = count;
348 if (!spi_transfer(&mc13783_spi, &xfer))
349 return -1;
351 return count - xfer.count;