10 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
20 #define LOG_PARSE(fmt...)
21 #define LOG_ERROR(fmt...) printk(fmt)
22 #define LOG_BLOB(t,b,c)
26 #define DBG(fmt...) printk(fmt)
32 #define PMF_CMD_LIST 0
33 #define PMF_CMD_WRITE_GPIO 1
34 #define PMF_CMD_READ_GPIO 2
35 #define PMF_CMD_WRITE_REG32 3
36 #define PMF_CMD_READ_REG32 4
37 #define PMF_CMD_WRITE_REG16 5
38 #define PMF_CMD_READ_REG16 6
39 #define PMF_CMD_WRITE_REG8 7
40 #define PMF_CMD_READ_REG8 8
41 #define PMF_CMD_DELAY 9
42 #define PMF_CMD_WAIT_REG32 10
43 #define PMF_CMD_WAIT_REG16 11
44 #define PMF_CMD_WAIT_REG8 12
45 #define PMF_CMD_READ_I2C 13
46 #define PMF_CMD_WRITE_I2C 14
47 #define PMF_CMD_RMW_I2C 15
48 #define PMF_CMD_GEN_I2C 16
49 #define PMF_CMD_SHIFT_BYTES_RIGHT 17
50 #define PMF_CMD_SHIFT_BYTES_LEFT 18
51 #define PMF_CMD_READ_CFG 19
52 #define PMF_CMD_WRITE_CFG 20
53 #define PMF_CMD_RMW_CFG 21
54 #define PMF_CMD_READ_I2C_SUBADDR 22
55 #define PMF_CMD_WRITE_I2C_SUBADDR 23
56 #define PMF_CMD_SET_I2C_MODE 24
57 #define PMF_CMD_RMW_I2C_SUBADDR 25
58 #define PMF_CMD_READ_REG32_MASK_SHR_XOR 26
59 #define PMF_CMD_READ_REG16_MASK_SHR_XOR 27
60 #define PMF_CMD_READ_REG8_MASK_SHR_XOR 28
61 #define PMF_CMD_WRITE_REG32_SHL_MASK 29
62 #define PMF_CMD_WRITE_REG16_SHL_MASK 30
63 #define PMF_CMD_WRITE_REG8_SHL_MASK 31
64 #define PMF_CMD_MASK_AND_COMPARE 32
65 #define PMF_CMD_COUNT 33
81 static void print_blob(
const char *title,
const void *
blob,
int bytes)
108 static const void* pmf_next_blob(
struct pmf_cmd *cmd,
int count)
124 #define PMF_PARSE_CALL(name, cmd, handlers, p...) \
128 if (handlers == NULL) \
130 if (handlers->name) \
131 return handlers->name(cmd->func, cmd->instdata, \
139 u8 value = (
u8)pmf_next32(cmd);
142 LOG_PARSE(
"pmf: write_gpio(value: %02x, mask: %02x)\n", value, mask);
150 int rshift = (
int)pmf_next32(cmd);
153 LOG_PARSE(
"pmf: read_gpio(mask: %02x, rshift: %d, xor: %02x)\n",
162 u32 value = pmf_next32(cmd);
163 u32 mask = pmf_next32(cmd);
165 LOG_PARSE(
"pmf: write_reg32(offset: %08x, value: %08x, mask: %08x)\n",
166 offset, value, mask);
175 LOG_PARSE(
"pmf: read_reg32(offset: %08x)\n", offset);
184 u16 value = (
u16)pmf_next32(cmd);
185 u16 mask = (
u16)pmf_next32(cmd);
187 LOG_PARSE(
"pmf: write_reg16(offset: %08x, value: %04x, mask: %04x)\n",
188 offset, value, mask);
195 u32 offset = pmf_next32(cmd);
197 LOG_PARSE(
"pmf: read_reg16(offset: %08x)\n", offset);
205 u32 offset = pmf_next32(cmd);
206 u8 value = (
u16)pmf_next32(cmd);
207 u8 mask = (
u16)pmf_next32(cmd);
209 LOG_PARSE(
"pmf: write_reg8(offset: %08x, value: %02x, mask: %02x)\n",
210 offset, value, mask);
217 u32 offset = pmf_next32(cmd);
219 LOG_PARSE(
"pmf: read_reg8(offset: %08x)\n", offset);
228 LOG_PARSE(
"pmf: delay(duration: %d us)\n", duration);
235 u32 offset = pmf_next32(cmd);
236 u32 value = pmf_next32(cmd);
237 u32 mask = pmf_next32(cmd);
239 LOG_PARSE(
"pmf: wait_reg32(offset: %08x, comp_value: %08x,mask: %08x)\n",
240 offset, value, mask);
247 u32 offset = pmf_next32(cmd);
248 u16 value = (
u16)pmf_next32(cmd);
249 u16 mask = (
u16)pmf_next32(cmd);
251 LOG_PARSE(
"pmf: wait_reg16(offset: %08x, comp_value: %04x,mask: %04x)\n",
252 offset, value, mask);
259 u32 offset = pmf_next32(cmd);
260 u8 value = (
u8)pmf_next32(cmd);
261 u8 mask = (
u8)pmf_next32(cmd);
263 LOG_PARSE(
"pmf: wait_reg8(offset: %08x, comp_value: %02x,mask: %02x)\n",
264 offset, value, mask);
273 LOG_PARSE(
"pmf: read_i2c(bytes: %ud)\n", bytes);
281 const void *
blob = pmf_next_blob(cmd, bytes);
283 LOG_PARSE(
"pmf: write_i2c(bytes: %ud) ...\n", bytes);
284 LOG_BLOB(
"pmf: data: \n", blob, bytes);
292 u32 maskbytes = pmf_next32(cmd);
293 u32 valuesbytes = pmf_next32(cmd);
294 u32 totalbytes = pmf_next32(cmd);
295 const void *maskblob = pmf_next_blob(cmd, maskbytes);
296 const void *valuesblob = pmf_next_blob(cmd, valuesbytes);
298 LOG_PARSE(
"pmf: rmw_i2c(maskbytes: %ud, valuebytes: %ud, "
299 "totalbytes: %d) ...\n",
300 maskbytes, valuesbytes, totalbytes);
301 LOG_BLOB(
"pmf: mask data: \n", maskblob, maskbytes);
302 LOG_BLOB(
"pmf: values data: \n", valuesblob, valuesbytes);
304 PMF_PARSE_CALL(rmw_i2c, cmd, h, maskbytes, valuesbytes, totalbytes,
305 maskblob, valuesblob);
310 u32 offset = pmf_next32(cmd);
313 LOG_PARSE(
"pmf: read_cfg(offset: %x, bytes: %ud)\n", offset, bytes);
321 u32 offset = pmf_next32(cmd);
323 const void *
blob = pmf_next_blob(cmd, bytes);
325 LOG_PARSE(
"pmf: write_cfg(offset: %x, bytes: %ud)\n", offset, bytes);
326 LOG_BLOB(
"pmf: data: \n", blob, bytes);
333 u32 offset = pmf_next32(cmd);
334 u32 maskbytes = pmf_next32(cmd);
335 u32 valuesbytes = pmf_next32(cmd);
336 u32 totalbytes = pmf_next32(cmd);
337 const void *maskblob = pmf_next_blob(cmd, maskbytes);
338 const void *valuesblob = pmf_next_blob(cmd, valuesbytes);
340 LOG_PARSE(
"pmf: rmw_cfg(maskbytes: %ud, valuebytes: %ud,"
341 " totalbytes: %d) ...\n",
342 maskbytes, valuesbytes, totalbytes);
343 LOG_BLOB(
"pmf: mask data: \n", maskblob, maskbytes);
344 LOG_BLOB(
"pmf: values data: \n", valuesblob, valuesbytes);
347 totalbytes, maskblob, valuesblob);
353 u8 subaddr = (
u8)pmf_next32(cmd);
356 LOG_PARSE(
"pmf: read_i2c_sub(subaddr: %x, bytes: %ud)\n",
364 u8 subaddr = (
u8)pmf_next32(cmd);
366 const void *
blob = pmf_next_blob(cmd, bytes);
368 LOG_PARSE(
"pmf: write_i2c_sub(subaddr: %x, bytes: %ud) ...\n",
370 LOG_BLOB(
"pmf: data: \n", blob, bytes);
379 LOG_PARSE(
"pmf: set_i2c_mode(mode: %d)\n", mode);
387 u8 subaddr = (
u8)pmf_next32(cmd);
388 u32 maskbytes = pmf_next32(cmd);
389 u32 valuesbytes = pmf_next32(cmd);
390 u32 totalbytes = pmf_next32(cmd);
391 const void *maskblob = pmf_next_blob(cmd, maskbytes);
392 const void *valuesblob = pmf_next_blob(cmd, valuesbytes);
394 LOG_PARSE(
"pmf: rmw_i2c_sub(subaddr: %x, maskbytes: %ud, valuebytes: %ud"
395 ", totalbytes: %d) ...\n",
396 subaddr, maskbytes, valuesbytes, totalbytes);
397 LOG_BLOB(
"pmf: mask data: \n", maskblob, maskbytes);
398 LOG_BLOB(
"pmf: values data: \n", valuesblob, valuesbytes);
400 PMF_PARSE_CALL(rmw_i2c_sub, cmd, h, subaddr, maskbytes, valuesbytes,
401 totalbytes, maskblob, valuesblob);
404 static int pmf_parser_read_reg32_msrx(
struct pmf_cmd *cmd,
407 u32 offset = pmf_next32(cmd);
408 u32 mask = pmf_next32(cmd);
409 u32 shift = pmf_next32(cmd);
410 u32 xor = pmf_next32(cmd);
412 LOG_PARSE(
"pmf: read_reg32_msrx(offset: %x, mask: %x, shift: %x,"
413 " xor: %x\n", offset, mask, shift, xor);
415 PMF_PARSE_CALL(read_reg32_msrx, cmd, h, offset, mask, shift, xor);
418 static int pmf_parser_read_reg16_msrx(
struct pmf_cmd *cmd,
421 u32 offset = pmf_next32(cmd);
422 u32 mask = pmf_next32(cmd);
423 u32 shift = pmf_next32(cmd);
424 u32 xor = pmf_next32(cmd);
426 LOG_PARSE(
"pmf: read_reg16_msrx(offset: %x, mask: %x, shift: %x,"
427 " xor: %x\n", offset, mask, shift, xor);
429 PMF_PARSE_CALL(read_reg16_msrx, cmd, h, offset, mask, shift, xor);
431 static int pmf_parser_read_reg8_msrx(
struct pmf_cmd *cmd,
434 u32 offset = pmf_next32(cmd);
435 u32 mask = pmf_next32(cmd);
436 u32 shift = pmf_next32(cmd);
437 u32 xor = pmf_next32(cmd);
439 LOG_PARSE(
"pmf: read_reg8_msrx(offset: %x, mask: %x, shift: %x,"
440 " xor: %x\n", offset, mask, shift, xor);
445 static int pmf_parser_write_reg32_slm(
struct pmf_cmd *cmd,
448 u32 offset = pmf_next32(cmd);
449 u32 shift = pmf_next32(cmd);
450 u32 mask = pmf_next32(cmd);
452 LOG_PARSE(
"pmf: write_reg32_slm(offset: %x, shift: %x, mask: %x\n",
453 offset, shift, mask);
458 static int pmf_parser_write_reg16_slm(
struct pmf_cmd *cmd,
461 u32 offset = pmf_next32(cmd);
462 u32 shift = pmf_next32(cmd);
463 u32 mask = pmf_next32(cmd);
465 LOG_PARSE(
"pmf: write_reg16_slm(offset: %x, shift: %x, mask: %x\n",
466 offset, shift, mask);
471 static int pmf_parser_write_reg8_slm(
struct pmf_cmd *cmd,
474 u32 offset = pmf_next32(cmd);
475 u32 shift = pmf_next32(cmd);
476 u32 mask = pmf_next32(cmd);
478 LOG_PARSE(
"pmf: write_reg8_slm(offset: %x, shift: %x, mask: %x\n",
479 offset, shift, mask);
484 static int pmf_parser_mask_and_compare(
struct pmf_cmd *cmd,
488 const void *maskblob = pmf_next_blob(cmd, bytes);
489 const void *valuesblob = pmf_next_blob(cmd, bytes);
491 LOG_PARSE(
"pmf: mask_and_compare(length: %ud ...\n", bytes);
492 LOG_BLOB(
"pmf: mask data: \n", maskblob, bytes);
493 LOG_BLOB(
"pmf: values data: \n", valuesblob, bytes);
496 bytes, maskblob, valuesblob);
505 pmf_parser_write_gpio,
506 pmf_parser_read_gpio,
507 pmf_parser_write_reg32,
508 pmf_parser_read_reg32,
509 pmf_parser_write_reg16,
510 pmf_parser_read_reg16,
511 pmf_parser_write_reg8,
512 pmf_parser_read_reg8,
514 pmf_parser_wait_reg32,
515 pmf_parser_wait_reg16,
516 pmf_parser_wait_reg8,
518 pmf_parser_write_i2c,
524 pmf_parser_write_cfg,
526 pmf_parser_read_i2c_sub,
527 pmf_parser_write_i2c_sub,
528 pmf_parser_set_i2c_mode,
529 pmf_parser_rmw_i2c_sub,
530 pmf_parser_read_reg32_msrx,
531 pmf_parser_read_reg16_msrx,
532 pmf_parser_read_reg8_msrx,
533 pmf_parser_write_reg32_slm,
534 pmf_parser_write_reg16_slm,
535 pmf_parser_write_reg8_slm,
536 pmf_parser_mask_and_compare,
551 static void pmf_release_device(
struct kref *
kref)
559 kref_put(&dev->
ref, pmf_release_device);
574 return pmf_get_device(dev);
581 void *instdata,
struct pmf_args *args)
594 LOG_PARSE(
"pmf: func %s, %d bytes, %s...\n",
596 handlers ?
"executing" :
"parsing");
603 ccode = pmf_next32(&cmd);
606 count = pmf_next32(&cmd) - 1;
607 ccode = pmf_next32(&cmd);
610 LOG_ERROR(
"pmf: parse error, not enough data\n");
614 LOG_ERROR(
"pmf: command code %d unknown !\n", ccode);
617 if (pmf_parsers[ccode] ==
NULL) {
618 LOG_ERROR(
"pmf: no parser for command %d !\n", ccode);
621 rc = pmf_parsers[ccode](&
cmd, handlers);
623 LOG_ERROR(
"pmf: parser for command %d returned"
624 " error %d\n", ccode, rc);
630 if (handlers ==
NULL)
636 static int pmf_add_function_prop(
struct pmf_device *dev,
void *driverdata,
643 DBG(
"pmf: Adding functions for platform-do-%s\n", name);
645 while (length >= 12) {
650 kref_init(&func->
ref);
656 func->
flags = data[1];
662 DBG(
"pmf: idx %d: flags=%08x, phandle=%08x "
663 " %d bytes remaining, parsing...\n",
676 DBG(
"pmf: Added %d functions\n", count);
681 static int pmf_add_functions(
struct pmf_device *dev,
void *driverdata)
684 #define PP_PREFIX "platform-do-"
688 for (pp = dev->
node->properties; pp != 0; pp = pp->
next) {
694 count += pmf_add_function_prop(dev, driverdata, name,
709 if (handlers ==
NULL)
712 DBG(
"pmf: registering driver for node %s\n", np->
full_name);
715 dev = pmf_find_device(np);
716 spin_unlock_irqrestore(&pmf_lock, flags);
718 DBG(
"pmf: already there !\n");
725 DBG(
"pmf: no memory !\n");
728 kref_init(&dev->
ref);
729 dev->
node = of_node_get(np);
733 rc = pmf_add_functions(dev, driverdata);
735 DBG(
"pmf: no functions, disposing.. \n");
742 list_add(&dev->
link, &pmf_devices);
743 spin_unlock_irqrestore(&pmf_lock, flags);
751 if (!try_module_get(func->
dev->handlers->owner))
753 kref_get(&func->
ref);
758 static void pmf_release_function(
struct kref *
kref)
762 pmf_put_device(func->
dev);
766 static inline void __pmf_put_function(
struct pmf_function *func)
768 kref_put(&func->
ref, pmf_release_function);
775 module_put(func->
dev->handlers->owner);
776 __pmf_put_function(func);
785 DBG(
"pmf: unregistering driver for node %s\n", np->
full_name);
788 dev = pmf_find_device(np);
790 DBG(
"pmf: not such driver !\n");
791 spin_unlock_irqrestore(&pmf_lock, flags);
800 __pmf_put_function(func);
804 spin_unlock_irqrestore(&pmf_lock, flags);
822 snprintf(fname, 63,
"platform-%s", name);
839 dev = pmf_find_device(actor);
850 if ((func->
flags & flags) == 0)
873 spin_unlock_irqrestore(&pmf_lock, flags);
880 func->
dev->handlers->irq_enable(func);
885 spin_unlock_irqrestore(&pmf_lock, flags);
908 spin_unlock_irqrestore(&pmf_lock, flags);
911 func->
dev->handlers->irq_disable(func);
928 if (!try_module_get(client->
owner))
931 module_put(client->
owner);
933 spin_unlock_irqrestore(&pmf_lock, flags);
941 void *instdata =
NULL;
944 DBG(
" ** pmf_call_one(%s/%s) **\n", dev->
node->full_name, func->
name);
947 instdata = dev->
handlers->begin(func, args);
948 rc = pmf_parse_one(func, dev->
handlers, instdata, args);
966 dev = pmf_find_device(np);
968 spin_unlock_irqrestore(&pmf_lock, flags);
976 if ((func->
flags & fflags) == 0)
980 spin_unlock_irqrestore(&pmf_lock, flags);
986 spin_unlock_irqrestore(&pmf_lock, flags);
1003 spin_unlock_irqrestore(&pmf_lock, flags);