pmic_core.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2011 Samsung Electronics
  3. * Lukasz Majewski <l.majewski@samsung.com>
  4. *
  5. * (C) Copyright 2010
  6. * Stefano Babic, DENX Software Engineering, sbabic@denx.de
  7. *
  8. * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <common.h>
  29. #include <malloc.h>
  30. #include <linux/types.h>
  31. #include <linux/list.h>
  32. #include <power/pmic.h>
  33. static LIST_HEAD(pmic_list);
  34. int check_reg(struct pmic *p, u32 reg)
  35. {
  36. if (reg >= p->number_of_regs) {
  37. printf("<reg num> = %d is invalid. Should be less than %d\n",
  38. reg, p->number_of_regs);
  39. return -1;
  40. }
  41. return 0;
  42. }
  43. int pmic_set_output(struct pmic *p, u32 reg, int out, int on)
  44. {
  45. u32 val;
  46. if (pmic_reg_read(p, reg, &val))
  47. return -1;
  48. if (on)
  49. val |= out;
  50. else
  51. val &= ~out;
  52. if (pmic_reg_write(p, reg, val))
  53. return -1;
  54. return 0;
  55. }
  56. static void pmic_show_info(struct pmic *p)
  57. {
  58. printf("PMIC: %s\n", p->name);
  59. }
  60. static int pmic_dump(struct pmic *p)
  61. {
  62. int i, ret;
  63. u32 val;
  64. if (!p) {
  65. puts("Wrong PMIC name!\n");
  66. return -1;
  67. }
  68. pmic_show_info(p);
  69. for (i = 0; i < p->number_of_regs; i++) {
  70. ret = pmic_reg_read(p, i, &val);
  71. if (ret)
  72. puts("PMIC: Registers dump failed\n");
  73. if (!(i % 8))
  74. printf("\n0x%02x: ", i);
  75. printf("%08x ", val);
  76. }
  77. puts("\n");
  78. return 0;
  79. }
  80. struct pmic *pmic_alloc(void)
  81. {
  82. struct pmic *p;
  83. p = calloc(sizeof(*p), 1);
  84. if (!p) {
  85. printf("%s: No available memory for allocation!\n", __func__);
  86. return NULL;
  87. }
  88. list_add_tail(&p->list, &pmic_list);
  89. debug("%s: new pmic struct: 0x%p\n", __func__, p);
  90. return p;
  91. }
  92. struct pmic *pmic_get(const char *s)
  93. {
  94. struct pmic *p;
  95. list_for_each_entry(p, &pmic_list, list) {
  96. if (strcmp(p->name, s) == 0) {
  97. debug("%s: pmic %s -> 0x%p\n", __func__, p->name, p);
  98. return p;
  99. }
  100. }
  101. return NULL;
  102. }
  103. static void pmic_list_names(void)
  104. {
  105. struct pmic *p;
  106. puts("PMIC devices:\n");
  107. list_for_each_entry(p, &pmic_list, list) {
  108. printf("name: %s\n", p->name);
  109. }
  110. }
  111. int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  112. {
  113. u32 ret, reg, val;
  114. struct pmic *p;
  115. char *cmd;
  116. /* at least two arguments please */
  117. if (argc < 2)
  118. return CMD_RET_USAGE;
  119. cmd = argv[1];
  120. if (strcmp(cmd, "list") == 0) {
  121. pmic_list_names();
  122. return CMD_RET_SUCCESS;
  123. }
  124. if (strcmp(cmd, "dump") == 0) {
  125. p = pmic_get(argv[2]);
  126. if (!p)
  127. return CMD_RET_FAILURE;
  128. if (pmic_dump(p))
  129. return CMD_RET_FAILURE;
  130. return CMD_RET_SUCCESS;
  131. }
  132. if (strcmp(cmd, "read") == 0) {
  133. if (argc < 4)
  134. return CMD_RET_USAGE;
  135. reg = simple_strtoul(argv[3], NULL, 16);
  136. p = pmic_get(argv[2]);
  137. if (!p)
  138. return CMD_RET_FAILURE;
  139. ret = pmic_reg_read(p, reg, &val);
  140. if (ret)
  141. puts("PMIC: Register read failed\n");
  142. printf("\n0x%02x: 0x%08x\n", reg, val);
  143. return CMD_RET_SUCCESS;
  144. }
  145. if (strcmp(cmd, "write") == 0) {
  146. if (argc < 5)
  147. return CMD_RET_USAGE;
  148. reg = simple_strtoul(argv[3], NULL, 16);
  149. val = simple_strtoul(argv[4], NULL, 16);
  150. p = pmic_get(argv[2]);
  151. if (!p)
  152. return CMD_RET_FAILURE;
  153. pmic_reg_write(p, reg, val);
  154. return CMD_RET_SUCCESS;
  155. }
  156. /* No subcommand found */
  157. return CMD_RET_SUCCESS;
  158. }
  159. U_BOOT_CMD(
  160. pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
  161. "PMIC",
  162. "list - list available PMICs\n"
  163. "pmic dump name - dump named PMIC registers\n"
  164. "pmic name read <reg> - read register\n"
  165. "pmic name write <reg> <value> - write register"
  166. );