3 * FIXME: Properly make this race free with refcounting etc...
8 #include <linux/config.h>
9 #include <linux/init.h>
10 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/spinlock.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
16 #include <asm/semaphore.h>
18 #include <asm/pmac_pfunc.h>
21 #define LOG_PARSE(fmt...)
22 #define LOG_ERROR(fmt...) printk(fmt)
23 #define LOG_BLOB(t,b,c)
27 #define DBG(fmt...) printk(fmt)
33 #define PMF_CMD_LIST 0
34 #define PMF_CMD_WRITE_GPIO 1
35 #define PMF_CMD_READ_GPIO 2
36 #define PMF_CMD_WRITE_REG32 3
37 #define PMF_CMD_READ_REG32 4
38 #define PMF_CMD_WRITE_REG16 5
39 #define PMF_CMD_READ_REG16 6
40 #define PMF_CMD_WRITE_REG8 7
41 #define PMF_CMD_READ_REG8 8
42 #define PMF_CMD_DELAY 9
43 #define PMF_CMD_WAIT_REG32 10
44 #define PMF_CMD_WAIT_REG16 11
45 #define PMF_CMD_WAIT_REG8 12
46 #define PMF_CMD_READ_I2C 13
47 #define PMF_CMD_WRITE_I2C 14
48 #define PMF_CMD_RMW_I2C 15
49 #define PMF_CMD_GEN_I2C 16
50 #define PMF_CMD_SHIFT_BYTES_RIGHT 17
51 #define PMF_CMD_SHIFT_BYTES_LEFT 18
52 #define PMF_CMD_READ_CFG 19
53 #define PMF_CMD_WRITE_CFG 20
54 #define PMF_CMD_RMW_CFG 21
55 #define PMF_CMD_READ_I2C_SUBADDR 22
56 #define PMF_CMD_WRITE_I2C_SUBADDR 23
57 #define PMF_CMD_SET_I2C_MODE 24
58 #define PMF_CMD_RMW_I2C_SUBADDR 25
59 #define PMF_CMD_READ_REG32_MASK_SHR_XOR 26
60 #define PMF_CMD_READ_REG16_MASK_SHR_XOR 27
61 #define PMF_CMD_READ_REG8_MASK_SHR_XOR 28
62 #define PMF_CMD_WRITE_REG32_SHL_MASK 29
63 #define PMF_CMD_WRITE_REG16_SHL_MASK 30
64 #define PMF_CMD_WRITE_REG8_SHL_MASK 31
65 #define PMF_CMD_MASK_AND_COMPARE 32
66 #define PMF_CMD_COUNT 33
68 /* This structure holds the state of the parser while walking through
69 * a function definition
74 struct pmf_function
*func
;
76 struct pmf_args
*args
;
82 static void print_blob(const char *title
, const void *blob
, int bytes
)
86 printk("%02x ", *((u8
*)blob
));
97 static u32
pmf_next32(struct pmf_cmd
*cmd
)
100 if ((cmd
->cmdend
- cmd
->cmdptr
) < 4) {
104 value
= *((u32
*)cmd
->cmdptr
);
109 static const void* pmf_next_blob(struct pmf_cmd
*cmd
, int count
)
112 if ((cmd
->cmdend
- cmd
->cmdptr
) < count
) {
117 cmd
->cmdptr
+= count
;
122 * Individual command parsers
125 #define PMF_PARSE_CALL(name, cmd, handlers, p...) \
129 if (handlers == NULL) \
131 if (handlers->name) \
132 return handlers->name(cmd->func, cmd->instdata, \
138 static int pmf_parser_write_gpio(struct pmf_cmd *cmd, struct pmf_handlers *h)
140 u8 value
= (u8
)pmf_next32(cmd
);
141 u8 mask
= (u8
)pmf_next32(cmd
);
143 LOG_PARSE("pmf: write_gpio(value: %02x, mask: %02x)\n", value
, mask
);
145 PMF_PARSE_CALL(write_gpio
, cmd
, h
, value
, mask
);
148 static int pmf_parser_read_gpio(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
150 u8 mask
= (u8
)pmf_next32(cmd
);
151 int rshift
= (int)pmf_next32(cmd
);
152 u8
xor = (u8
)pmf_next32(cmd
);
154 LOG_PARSE("pmf: read_gpio(mask: %02x, rshift: %d, xor: %02x)\n",
157 PMF_PARSE_CALL(read_gpio
, cmd
, h
, mask
, rshift
, xor);
160 static int pmf_parser_write_reg32(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
162 u32 offset
= pmf_next32(cmd
);
163 u32 value
= pmf_next32(cmd
);
164 u32 mask
= pmf_next32(cmd
);
166 LOG_PARSE("pmf: write_reg32(offset: %08x, value: %08x, mask: %08x)\n",
167 offset
, value
, mask
);
169 PMF_PARSE_CALL(write_reg32
, cmd
, h
, offset
, value
, mask
);
172 static int pmf_parser_read_reg32(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
174 u32 offset
= pmf_next32(cmd
);
176 LOG_PARSE("pmf: read_reg32(offset: %08x)\n", offset
);
178 PMF_PARSE_CALL(read_reg32
, cmd
, h
, offset
);
182 static int pmf_parser_write_reg16(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
184 u32 offset
= pmf_next32(cmd
);
185 u16 value
= (u16
)pmf_next32(cmd
);
186 u16 mask
= (u16
)pmf_next32(cmd
);
188 LOG_PARSE("pmf: write_reg16(offset: %08x, value: %04x, mask: %04x)\n",
189 offset
, value
, mask
);
191 PMF_PARSE_CALL(write_reg16
, cmd
, h
, offset
, value
, mask
);
194 static int pmf_parser_read_reg16(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
196 u32 offset
= pmf_next32(cmd
);
198 LOG_PARSE("pmf: read_reg16(offset: %08x)\n", offset
);
200 PMF_PARSE_CALL(read_reg16
, cmd
, h
, offset
);
204 static int pmf_parser_write_reg8(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
206 u32 offset
= pmf_next32(cmd
);
207 u8 value
= (u16
)pmf_next32(cmd
);
208 u8 mask
= (u16
)pmf_next32(cmd
);
210 LOG_PARSE("pmf: write_reg8(offset: %08x, value: %02x, mask: %02x)\n",
211 offset
, value
, mask
);
213 PMF_PARSE_CALL(write_reg8
, cmd
, h
, offset
, value
, mask
);
216 static int pmf_parser_read_reg8(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
218 u32 offset
= pmf_next32(cmd
);
220 LOG_PARSE("pmf: read_reg8(offset: %08x)\n", offset
);
222 PMF_PARSE_CALL(read_reg8
, cmd
, h
, offset
);
225 static int pmf_parser_delay(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
227 u32 duration
= pmf_next32(cmd
);
229 LOG_PARSE("pmf: delay(duration: %d us)\n", duration
);
231 PMF_PARSE_CALL(delay
, cmd
, h
, duration
);
234 static int pmf_parser_wait_reg32(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
236 u32 offset
= pmf_next32(cmd
);
237 u32 value
= pmf_next32(cmd
);
238 u32 mask
= pmf_next32(cmd
);
240 LOG_PARSE("pmf: wait_reg32(offset: %08x, comp_value: %08x,mask: %08x)\n",
241 offset
, value
, mask
);
243 PMF_PARSE_CALL(wait_reg32
, cmd
, h
, offset
, value
, mask
);
246 static int pmf_parser_wait_reg16(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
248 u32 offset
= pmf_next32(cmd
);
249 u16 value
= (u16
)pmf_next32(cmd
);
250 u16 mask
= (u16
)pmf_next32(cmd
);
252 LOG_PARSE("pmf: wait_reg16(offset: %08x, comp_value: %04x,mask: %04x)\n",
253 offset
, value
, mask
);
255 PMF_PARSE_CALL(wait_reg16
, cmd
, h
, offset
, value
, mask
);
258 static int pmf_parser_wait_reg8(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
260 u32 offset
= pmf_next32(cmd
);
261 u8 value
= (u8
)pmf_next32(cmd
);
262 u8 mask
= (u8
)pmf_next32(cmd
);
264 LOG_PARSE("pmf: wait_reg8(offset: %08x, comp_value: %02x,mask: %02x)\n",
265 offset
, value
, mask
);
267 PMF_PARSE_CALL(wait_reg8
, cmd
, h
, offset
, value
, mask
);
270 static int pmf_parser_read_i2c(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
272 u32 bytes
= pmf_next32(cmd
);
274 LOG_PARSE("pmf: read_i2c(bytes: %ud)\n", bytes
);
276 PMF_PARSE_CALL(read_i2c
, cmd
, h
, bytes
);
279 static int pmf_parser_write_i2c(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
281 u32 bytes
= pmf_next32(cmd
);
282 const void *blob
= pmf_next_blob(cmd
, bytes
);
284 LOG_PARSE("pmf: write_i2c(bytes: %ud) ...\n", bytes
);
285 LOG_BLOB("pmf: data: \n", blob
, bytes
);
287 PMF_PARSE_CALL(write_i2c
, cmd
, h
, bytes
, blob
);
291 static int pmf_parser_rmw_i2c(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
293 u32 maskbytes
= pmf_next32(cmd
);
294 u32 valuesbytes
= pmf_next32(cmd
);
295 u32 totalbytes
= pmf_next32(cmd
);
296 const void *maskblob
= pmf_next_blob(cmd
, maskbytes
);
297 const void *valuesblob
= pmf_next_blob(cmd
, valuesbytes
);
299 LOG_PARSE("pmf: rmw_i2c(maskbytes: %ud, valuebytes: %ud, "
300 "totalbytes: %d) ...\n",
301 maskbytes
, valuesbytes
, totalbytes
);
302 LOG_BLOB("pmf: mask data: \n", maskblob
, maskbytes
);
303 LOG_BLOB("pmf: values data: \n", valuesblob
, valuesbytes
);
305 PMF_PARSE_CALL(rmw_i2c
, cmd
, h
, maskbytes
, valuesbytes
, totalbytes
,
306 maskblob
, valuesblob
);
309 static int pmf_parser_read_cfg(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
311 u32 offset
= pmf_next32(cmd
);
312 u32 bytes
= pmf_next32(cmd
);
314 LOG_PARSE("pmf: read_cfg(offset: %x, bytes: %ud)\n", offset
, bytes
);
316 PMF_PARSE_CALL(read_cfg
, cmd
, h
, offset
, bytes
);
320 static int pmf_parser_write_cfg(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
322 u32 offset
= pmf_next32(cmd
);
323 u32 bytes
= pmf_next32(cmd
);
324 const void *blob
= pmf_next_blob(cmd
, bytes
);
326 LOG_PARSE("pmf: write_cfg(offset: %x, bytes: %ud)\n", offset
, bytes
);
327 LOG_BLOB("pmf: data: \n", blob
, bytes
);
329 PMF_PARSE_CALL(write_cfg
, cmd
, h
, offset
, bytes
, blob
);
332 static int pmf_parser_rmw_cfg(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
334 u32 offset
= pmf_next32(cmd
);
335 u32 maskbytes
= pmf_next32(cmd
);
336 u32 valuesbytes
= pmf_next32(cmd
);
337 u32 totalbytes
= pmf_next32(cmd
);
338 const void *maskblob
= pmf_next_blob(cmd
, maskbytes
);
339 const void *valuesblob
= pmf_next_blob(cmd
, valuesbytes
);
341 LOG_PARSE("pmf: rmw_cfg(maskbytes: %ud, valuebytes: %ud,"
342 " totalbytes: %d) ...\n",
343 maskbytes
, valuesbytes
, totalbytes
);
344 LOG_BLOB("pmf: mask data: \n", maskblob
, maskbytes
);
345 LOG_BLOB("pmf: values data: \n", valuesblob
, valuesbytes
);
347 PMF_PARSE_CALL(rmw_cfg
, cmd
, h
, offset
, maskbytes
, valuesbytes
,
348 totalbytes
, maskblob
, valuesblob
);
352 static int pmf_parser_read_i2c_sub(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
354 u8 subaddr
= (u8
)pmf_next32(cmd
);
355 u32 bytes
= pmf_next32(cmd
);
357 LOG_PARSE("pmf: read_i2c_sub(subaddr: %x, bytes: %ud)\n",
360 PMF_PARSE_CALL(read_i2c_sub
, cmd
, h
, subaddr
, bytes
);
363 static int pmf_parser_write_i2c_sub(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
365 u8 subaddr
= (u8
)pmf_next32(cmd
);
366 u32 bytes
= pmf_next32(cmd
);
367 const void *blob
= pmf_next_blob(cmd
, bytes
);
369 LOG_PARSE("pmf: write_i2c_sub(subaddr: %x, bytes: %ud) ...\n",
371 LOG_BLOB("pmf: data: \n", blob
, bytes
);
373 PMF_PARSE_CALL(write_i2c_sub
, cmd
, h
, subaddr
, bytes
, blob
);
376 static int pmf_parser_set_i2c_mode(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
378 u32 mode
= pmf_next32(cmd
);
380 LOG_PARSE("pmf: set_i2c_mode(mode: %d)\n", mode
);
382 PMF_PARSE_CALL(set_i2c_mode
, cmd
, h
, mode
);
386 static int pmf_parser_rmw_i2c_sub(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
)
388 u8 subaddr
= (u8
)pmf_next32(cmd
);
389 u32 maskbytes
= pmf_next32(cmd
);
390 u32 valuesbytes
= pmf_next32(cmd
);
391 u32 totalbytes
= pmf_next32(cmd
);
392 const void *maskblob
= pmf_next_blob(cmd
, maskbytes
);
393 const void *valuesblob
= pmf_next_blob(cmd
, valuesbytes
);
395 LOG_PARSE("pmf: rmw_i2c_sub(subaddr: %x, maskbytes: %ud, valuebytes: %ud"
396 ", totalbytes: %d) ...\n",
397 subaddr
, maskbytes
, valuesbytes
, totalbytes
);
398 LOG_BLOB("pmf: mask data: \n", maskblob
, maskbytes
);
399 LOG_BLOB("pmf: values data: \n", valuesblob
, valuesbytes
);
401 PMF_PARSE_CALL(rmw_i2c_sub
, cmd
, h
, subaddr
, maskbytes
, valuesbytes
,
402 totalbytes
, maskblob
, valuesblob
);
405 static int pmf_parser_read_reg32_msrx(struct pmf_cmd
*cmd
,
406 struct pmf_handlers
*h
)
408 u32 offset
= pmf_next32(cmd
);
409 u32 mask
= pmf_next32(cmd
);
410 u32 shift
= pmf_next32(cmd
);
411 u32
xor = pmf_next32(cmd
);
413 LOG_PARSE("pmf: read_reg32_msrx(offset: %x, mask: %x, shift: %x,"
414 " xor: %x\n", offset
, mask
, shift
, xor);
416 PMF_PARSE_CALL(read_reg32_msrx
, cmd
, h
, offset
, mask
, shift
, xor);
419 static int pmf_parser_read_reg16_msrx(struct pmf_cmd
*cmd
,
420 struct pmf_handlers
*h
)
422 u32 offset
= pmf_next32(cmd
);
423 u32 mask
= pmf_next32(cmd
);
424 u32 shift
= pmf_next32(cmd
);
425 u32
xor = pmf_next32(cmd
);
427 LOG_PARSE("pmf: read_reg16_msrx(offset: %x, mask: %x, shift: %x,"
428 " xor: %x\n", offset
, mask
, shift
, xor);
430 PMF_PARSE_CALL(read_reg16_msrx
, cmd
, h
, offset
, mask
, shift
, xor);
432 static int pmf_parser_read_reg8_msrx(struct pmf_cmd
*cmd
,
433 struct pmf_handlers
*h
)
435 u32 offset
= pmf_next32(cmd
);
436 u32 mask
= pmf_next32(cmd
);
437 u32 shift
= pmf_next32(cmd
);
438 u32
xor = pmf_next32(cmd
);
440 LOG_PARSE("pmf: read_reg8_msrx(offset: %x, mask: %x, shift: %x,"
441 " xor: %x\n", offset
, mask
, shift
, xor);
443 PMF_PARSE_CALL(read_reg8_msrx
, cmd
, h
, offset
, mask
, shift
, xor);
446 static int pmf_parser_write_reg32_slm(struct pmf_cmd
*cmd
,
447 struct pmf_handlers
*h
)
449 u32 offset
= pmf_next32(cmd
);
450 u32 shift
= pmf_next32(cmd
);
451 u32 mask
= pmf_next32(cmd
);
453 LOG_PARSE("pmf: write_reg32_slm(offset: %x, shift: %x, mask: %x\n",
454 offset
, shift
, mask
);
456 PMF_PARSE_CALL(write_reg32_slm
, cmd
, h
, offset
, shift
, mask
);
459 static int pmf_parser_write_reg16_slm(struct pmf_cmd
*cmd
,
460 struct pmf_handlers
*h
)
462 u32 offset
= pmf_next32(cmd
);
463 u32 shift
= pmf_next32(cmd
);
464 u32 mask
= pmf_next32(cmd
);
466 LOG_PARSE("pmf: write_reg16_slm(offset: %x, shift: %x, mask: %x\n",
467 offset
, shift
, mask
);
469 PMF_PARSE_CALL(write_reg16_slm
, cmd
, h
, offset
, shift
, mask
);
472 static int pmf_parser_write_reg8_slm(struct pmf_cmd
*cmd
,
473 struct pmf_handlers
*h
)
475 u32 offset
= pmf_next32(cmd
);
476 u32 shift
= pmf_next32(cmd
);
477 u32 mask
= pmf_next32(cmd
);
479 LOG_PARSE("pmf: write_reg8_slm(offset: %x, shift: %x, mask: %x\n",
480 offset
, shift
, mask
);
482 PMF_PARSE_CALL(write_reg8_slm
, cmd
, h
, offset
, shift
, mask
);
485 static int pmf_parser_mask_and_compare(struct pmf_cmd
*cmd
,
486 struct pmf_handlers
*h
)
488 u32 bytes
= pmf_next32(cmd
);
489 const void *maskblob
= pmf_next_blob(cmd
, bytes
);
490 const void *valuesblob
= pmf_next_blob(cmd
, bytes
);
492 LOG_PARSE("pmf: mask_and_compare(length: %ud ...\n", bytes
);
493 LOG_BLOB("pmf: mask data: \n", maskblob
, bytes
);
494 LOG_BLOB("pmf: values data: \n", valuesblob
, bytes
);
496 PMF_PARSE_CALL(mask_and_compare
, cmd
, h
,
497 bytes
, maskblob
, valuesblob
);
501 typedef int (*pmf_cmd_parser_t
)(struct pmf_cmd
*cmd
, struct pmf_handlers
*h
);
503 static pmf_cmd_parser_t pmf_parsers
[PMF_CMD_COUNT
] =
506 pmf_parser_write_gpio
,
507 pmf_parser_read_gpio
,
508 pmf_parser_write_reg32
,
509 pmf_parser_read_reg32
,
510 pmf_parser_write_reg16
,
511 pmf_parser_read_reg16
,
512 pmf_parser_write_reg8
,
513 pmf_parser_read_reg8
,
515 pmf_parser_wait_reg32
,
516 pmf_parser_wait_reg16
,
517 pmf_parser_wait_reg8
,
519 pmf_parser_write_i2c
,
521 NULL
, /* Bogus command */
522 NULL
, /* Shift bytes right: NYI */
523 NULL
, /* Shift bytes left: NYI */
525 pmf_parser_write_cfg
,
527 pmf_parser_read_i2c_sub
,
528 pmf_parser_write_i2c_sub
,
529 pmf_parser_set_i2c_mode
,
530 pmf_parser_rmw_i2c_sub
,
531 pmf_parser_read_reg32_msrx
,
532 pmf_parser_read_reg16_msrx
,
533 pmf_parser_read_reg8_msrx
,
534 pmf_parser_write_reg32_slm
,
535 pmf_parser_write_reg16_slm
,
536 pmf_parser_write_reg8_slm
,
537 pmf_parser_mask_and_compare
,
541 struct list_head link
;
542 struct device_node
*node
;
543 struct pmf_handlers
*handlers
;
544 struct list_head functions
;
548 static LIST_HEAD(pmf_devices
);
549 static spinlock_t pmf_lock
= SPIN_LOCK_UNLOCKED
;
550 static DEFINE_MUTEX(pmf_irq_mutex
);
552 static void pmf_release_device(struct kref
*kref
)
554 struct pmf_device
*dev
= container_of(kref
, struct pmf_device
, ref
);
558 static inline void pmf_put_device(struct pmf_device
*dev
)
560 kref_put(&dev
->ref
, pmf_release_device
);
563 static inline struct pmf_device
*pmf_get_device(struct pmf_device
*dev
)
569 static inline struct pmf_device
*pmf_find_device(struct device_node
*np
)
571 struct pmf_device
*dev
;
573 list_for_each_entry(dev
, &pmf_devices
, link
) {
575 return pmf_get_device(dev
);
580 static int pmf_parse_one(struct pmf_function
*func
,
581 struct pmf_handlers
*handlers
,
582 void *instdata
, struct pmf_args
*args
)
588 cmd
.cmdptr
= func
->data
;
589 cmd
.cmdend
= func
->data
+ func
->length
;
591 cmd
.instdata
= instdata
;
595 LOG_PARSE("pmf: func %s, %d bytes, %s...\n",
596 func
->name
, func
->length
,
597 handlers
? "executing" : "parsing");
599 /* One subcommand to parse for now */
602 while(count
-- && cmd
.cmdptr
< cmd
.cmdend
) {
604 ccode
= pmf_next32(&cmd
);
605 /* Check if we are hitting a command list, fetch new count */
607 count
= pmf_next32(&cmd
) - 1;
608 ccode
= pmf_next32(&cmd
);
611 LOG_ERROR("pmf: parse error, not enough data\n");
614 if (ccode
>= PMF_CMD_COUNT
) {
615 LOG_ERROR("pmf: command code %d unknown !\n", ccode
);
618 if (pmf_parsers
[ccode
] == NULL
) {
619 LOG_ERROR("pmf: no parser for command %d !\n", ccode
);
622 rc
= pmf_parsers
[ccode
](&cmd
, handlers
);
624 LOG_ERROR("pmf: parser for command %d returned"
625 " error %d\n", ccode
, rc
);
630 /* We are doing an initial parse pass, we need to adjust the size */
631 if (handlers
== NULL
)
632 func
->length
= cmd
.cmdptr
- func
->data
;
637 static int pmf_add_function_prop(struct pmf_device
*dev
, void *driverdata
,
638 const char *name
, u32
*data
,
642 struct pmf_function
*func
= NULL
;
644 DBG("pmf: Adding functions for platform-do-%s\n", name
);
646 while (length
>= 12) {
647 /* Allocate a structure */
648 func
= kzalloc(sizeof(struct pmf_function
), GFP_KERNEL
);
651 kref_init(&func
->ref
);
652 INIT_LIST_HEAD(&func
->irq_clients
);
653 func
->node
= dev
->node
;
654 func
->driver_data
= driverdata
;
656 func
->phandle
= data
[0];
657 func
->flags
= data
[1];
661 func
->length
= length
;
663 DBG("pmf: idx %d: flags=%08x, phandle=%08x "
664 " %d bytes remaining, parsing...\n",
665 count
+1, func
->flags
, func
->phandle
, length
);
666 if (pmf_parse_one(func
, NULL
, NULL
, NULL
)) {
670 length
-= func
->length
;
671 data
= (u32
*)(((u8
*)data
) + func
->length
);
672 list_add(&func
->link
, &dev
->functions
);
677 DBG("pmf: Added %d functions\n", count
);
682 static int pmf_add_functions(struct pmf_device
*dev
, void *driverdata
)
685 #define PP_PREFIX "platform-do-"
686 const int plen
= strlen(PP_PREFIX
);
689 for (pp
= dev
->node
->properties
; pp
!= 0; pp
= pp
->next
) {
691 if (strncmp(pp
->name
, PP_PREFIX
, plen
) != 0)
693 name
= pp
->name
+ plen
;
694 if (strlen(name
) && pp
->length
>= 12)
695 count
+= pmf_add_function_prop(dev
, driverdata
, name
,
703 int pmf_register_driver(struct device_node
*np
,
704 struct pmf_handlers
*handlers
,
707 struct pmf_device
*dev
;
711 if (handlers
== NULL
)
714 DBG("pmf: registering driver for node %s\n", np
->full_name
);
716 spin_lock_irqsave(&pmf_lock
, flags
);
717 dev
= pmf_find_device(np
);
718 spin_unlock_irqrestore(&pmf_lock
, flags
);
720 DBG("pmf: already there !\n");
725 dev
= kzalloc(sizeof(struct pmf_device
), GFP_KERNEL
);
727 DBG("pmf: no memory !\n");
730 kref_init(&dev
->ref
);
731 dev
->node
= of_node_get(np
);
732 dev
->handlers
= handlers
;
733 INIT_LIST_HEAD(&dev
->functions
);
735 rc
= pmf_add_functions(dev
, driverdata
);
737 DBG("pmf: no functions, disposing.. \n");
743 spin_lock_irqsave(&pmf_lock
, flags
);
744 list_add(&dev
->link
, &pmf_devices
);
745 spin_unlock_irqrestore(&pmf_lock
, flags
);
749 EXPORT_SYMBOL_GPL(pmf_register_driver
);
751 struct pmf_function
*pmf_get_function(struct pmf_function
*func
)
753 if (!try_module_get(func
->dev
->handlers
->owner
))
755 kref_get(&func
->ref
);
758 EXPORT_SYMBOL_GPL(pmf_get_function
);
760 static void pmf_release_function(struct kref
*kref
)
762 struct pmf_function
*func
=
763 container_of(kref
, struct pmf_function
, ref
);
764 pmf_put_device(func
->dev
);
768 static inline void __pmf_put_function(struct pmf_function
*func
)
770 kref_put(&func
->ref
, pmf_release_function
);
773 void pmf_put_function(struct pmf_function
*func
)
777 module_put(func
->dev
->handlers
->owner
);
778 __pmf_put_function(func
);
780 EXPORT_SYMBOL_GPL(pmf_put_function
);
782 void pmf_unregister_driver(struct device_node
*np
)
784 struct pmf_device
*dev
;
787 DBG("pmf: unregistering driver for node %s\n", np
->full_name
);
789 spin_lock_irqsave(&pmf_lock
, flags
);
790 dev
= pmf_find_device(np
);
792 DBG("pmf: not such driver !\n");
793 spin_unlock_irqrestore(&pmf_lock
, flags
);
796 list_del(&dev
->link
);
798 while(!list_empty(&dev
->functions
)) {
799 struct pmf_function
*func
=
800 list_entry(dev
->functions
.next
, typeof(*func
), link
);
801 list_del(&func
->link
);
802 __pmf_put_function(func
);
806 spin_unlock_irqrestore(&pmf_lock
, flags
);
808 EXPORT_SYMBOL_GPL(pmf_unregister_driver
);
810 struct pmf_function
*__pmf_find_function(struct device_node
*target
,
811 const char *name
, u32 flags
)
813 struct device_node
*actor
= of_node_get(target
);
814 struct pmf_device
*dev
;
815 struct pmf_function
*func
, *result
= NULL
;
820 * Look for a "platform-*" function reference. If we can't find
821 * one, then we fallback to a direct call attempt
823 snprintf(fname
, 63, "platform-%s", name
);
824 prop
= (u32
*)get_property(target
, fname
, NULL
);
832 * Ok, now try to find the actor. If we can't find it, we fail,
833 * there is no point in falling back there
836 actor
= of_find_node_by_phandle(ph
);
840 dev
= pmf_find_device(actor
);
844 list_for_each_entry(func
, &dev
->functions
, link
) {
845 if (name
&& strcmp(name
, func
->name
))
847 if (func
->phandle
&& target
->node
!= func
->phandle
)
849 if ((func
->flags
& flags
) == 0)
860 int pmf_register_irq_client(struct device_node
*target
,
862 struct pmf_irq_client
*client
)
864 struct pmf_function
*func
;
867 spin_lock_irqsave(&pmf_lock
, flags
);
868 func
= __pmf_find_function(target
, name
, PMF_FLAGS_INT_GEN
);
870 func
= pmf_get_function(func
);
871 spin_unlock_irqrestore(&pmf_lock
, flags
);
874 mutex_lock(&pmf_irq_mutex
);
875 if (list_empty(&func
->irq_clients
))
876 func
->dev
->handlers
->irq_enable(func
);
877 list_add(&client
->link
, &func
->irq_clients
);
879 mutex_unlock(&pmf_irq_mutex
);
883 EXPORT_SYMBOL_GPL(pmf_register_irq_client
);
885 void pmf_unregister_irq_client(struct pmf_irq_client
*client
)
887 struct pmf_function
*func
= client
->func
;
889 BUG_ON(func
== NULL
);
891 mutex_lock(&pmf_irq_mutex
);
893 list_del(&client
->link
);
894 if (list_empty(&func
->irq_clients
))
895 func
->dev
->handlers
->irq_disable(func
);
896 mutex_unlock(&pmf_irq_mutex
);
897 pmf_put_function(func
);
899 EXPORT_SYMBOL_GPL(pmf_unregister_irq_client
);
902 void pmf_do_irq(struct pmf_function
*func
)
905 struct pmf_irq_client
*client
;
907 /* For now, using a spinlock over the whole function. Can be made
908 * to drop the lock using 2 lists if necessary
910 spin_lock_irqsave(&pmf_lock
, flags
);
911 list_for_each_entry(client
, &func
->irq_clients
, link
) {
912 if (!try_module_get(client
->owner
))
914 client
->handler(client
->data
);
915 module_put(client
->owner
);
917 spin_unlock_irqrestore(&pmf_lock
, flags
);
919 EXPORT_SYMBOL_GPL(pmf_do_irq
);
922 int pmf_call_one(struct pmf_function
*func
, struct pmf_args
*args
)
924 struct pmf_device
*dev
= func
->dev
;
925 void *instdata
= NULL
;
928 DBG(" ** pmf_call_one(%s/%s) **\n", dev
->node
->full_name
, func
->name
);
930 if (dev
->handlers
->begin
)
931 instdata
= dev
->handlers
->begin(func
, args
);
932 rc
= pmf_parse_one(func
, dev
->handlers
, instdata
, args
);
933 if (dev
->handlers
->end
)
934 dev
->handlers
->end(func
, instdata
);
938 EXPORT_SYMBOL_GPL(pmf_call_one
);
940 int pmf_do_functions(struct device_node
*np
, const char *name
,
941 u32 phandle
, u32 fflags
, struct pmf_args
*args
)
943 struct pmf_device
*dev
;
944 struct pmf_function
*func
, *tmp
;
948 spin_lock_irqsave(&pmf_lock
, flags
);
950 dev
= pmf_find_device(np
);
952 spin_unlock_irqrestore(&pmf_lock
, flags
);
955 list_for_each_entry_safe(func
, tmp
, &dev
->functions
, link
) {
956 if (name
&& strcmp(name
, func
->name
))
958 if (phandle
&& func
->phandle
&& phandle
!= func
->phandle
)
960 if ((func
->flags
& fflags
) == 0)
962 if (pmf_get_function(func
) == NULL
)
964 spin_unlock_irqrestore(&pmf_lock
, flags
);
965 rc
= pmf_call_one(func
, args
);
966 pmf_put_function(func
);
967 spin_lock_irqsave(&pmf_lock
, flags
);
970 spin_unlock_irqrestore(&pmf_lock
, flags
);
974 EXPORT_SYMBOL_GPL(pmf_do_functions
);
977 struct pmf_function
*pmf_find_function(struct device_node
*target
,
980 struct pmf_function
*func
;
983 spin_lock_irqsave(&pmf_lock
, flags
);
984 func
= __pmf_find_function(target
, name
, PMF_FLAGS_ON_DEMAND
);
986 func
= pmf_get_function(func
);
987 spin_unlock_irqrestore(&pmf_lock
, flags
);
990 EXPORT_SYMBOL_GPL(pmf_find_function
);
992 int pmf_call_function(struct device_node
*target
, const char *name
,
993 struct pmf_args
*args
)
995 struct pmf_function
*func
= pmf_find_function(target
, name
);
1001 rc
= pmf_call_one(func
, args
);
1002 pmf_put_function(func
);
1005 EXPORT_SYMBOL_GPL(pmf_call_function
);