pmic_core.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. const char *power_get_interface(int interface)
  104. {
  105. const char *power_interface[] = {"I2C", "SPI", "|+|-|"};
  106. return power_interface[interface];
  107. }
  108. static void pmic_list_names(void)
  109. {
  110. struct pmic *p;
  111. puts("PMIC devices:\n");
  112. list_for_each_entry(p, &pmic_list, list) {
  113. printf("name: %s bus: %s_%d\n", p->name,
  114. power_get_interface(p->interface), p->bus);
  115. }
  116. }
  117. int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  118. {
  119. u32 ret, reg, val;
  120. char *cmd, *name;
  121. struct pmic *p;
  122. /* at least two arguments please */
  123. if (argc < 2)
  124. return CMD_RET_USAGE;
  125. if (strcmp(argv[1], "list") == 0) {
  126. pmic_list_names();
  127. return CMD_RET_SUCCESS;
  128. }
  129. name = argv[1];
  130. cmd = argv[2];
  131. debug("%s: name: %s cmd: %s\n", __func__, name, cmd);
  132. p = pmic_get(name);
  133. if (!p)
  134. return CMD_RET_FAILURE;
  135. if (strcmp(cmd, "dump") == 0) {
  136. if (pmic_dump(p))
  137. return CMD_RET_FAILURE;
  138. return CMD_RET_SUCCESS;
  139. }
  140. if (strcmp(cmd, "read") == 0) {
  141. if (argc < 4)
  142. return CMD_RET_USAGE;
  143. reg = simple_strtoul(argv[3], NULL, 16);
  144. ret = pmic_reg_read(p, reg, &val);
  145. if (ret)
  146. puts("PMIC: Register read failed\n");
  147. printf("\n0x%02x: 0x%08x\n", reg, val);
  148. return CMD_RET_SUCCESS;
  149. }
  150. if (strcmp(cmd, "write") == 0) {
  151. if (argc < 5)
  152. return CMD_RET_USAGE;
  153. reg = simple_strtoul(argv[3], NULL, 16);
  154. val = simple_strtoul(argv[4], NULL, 16);
  155. pmic_reg_write(p, reg, val);
  156. return CMD_RET_SUCCESS;
  157. }
  158. if (strcmp(cmd, "bat") == 0) {
  159. if (argc < 4)
  160. return CMD_RET_USAGE;
  161. if (strcmp(argv[3], "state") == 0)
  162. p->fg->fg_battery_check(p->pbat->fg, p);
  163. if (strcmp(argv[3], "charge") == 0) {
  164. if (p->pbat) {
  165. printf("PRINT BAT charge %s\n", p->name);
  166. if (p->low_power_mode)
  167. p->low_power_mode();
  168. if (p->pbat->battery_charge)
  169. p->pbat->battery_charge(p);
  170. }
  171. }
  172. return CMD_RET_SUCCESS;
  173. }
  174. /* No subcommand found */
  175. return CMD_RET_SUCCESS;
  176. }
  177. U_BOOT_CMD(
  178. pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
  179. "PMIC",
  180. "list - list available PMICs\n"
  181. "pmic name dump - dump named PMIC registers\n"
  182. "pmic name read <reg> - read register\n"
  183. "pmic name write <reg> <value> - write register\n"
  184. "pmic name bat state - write register\n"
  185. "pmic name bat charge - write register\n"
  186. );