pmic_core.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <linux/types.h>
  30. #include <pmic.h>
  31. static struct pmic pmic;
  32. int check_reg(u32 reg)
  33. {
  34. if (reg >= pmic.number_of_regs) {
  35. printf("<reg num> = %d is invalid. Should be less than %d\n",
  36. reg, pmic.number_of_regs);
  37. return -1;
  38. }
  39. return 0;
  40. }
  41. int pmic_set_output(struct pmic *p, u32 reg, int out, int on)
  42. {
  43. u32 val;
  44. if (pmic_reg_read(p, reg, &val))
  45. return -1;
  46. if (on)
  47. val |= out;
  48. else
  49. val &= ~out;
  50. if (pmic_reg_write(p, reg, val))
  51. return -1;
  52. return 0;
  53. }
  54. static void pmic_show_info(struct pmic *p)
  55. {
  56. printf("PMIC: %s\n", p->name);
  57. }
  58. static void pmic_dump(struct pmic *p)
  59. {
  60. int i, ret;
  61. u32 val;
  62. pmic_show_info(p);
  63. for (i = 0; i < p->number_of_regs; i++) {
  64. ret = pmic_reg_read(p, i, &val);
  65. if (ret)
  66. puts("PMIC: Registers dump failed\n");
  67. if (!(i % 8))
  68. printf("\n0x%02x: ", i);
  69. printf("%08x ", val);
  70. }
  71. puts("\n");
  72. }
  73. struct pmic *get_pmic(void)
  74. {
  75. return &pmic;
  76. }
  77. int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  78. {
  79. u32 ret, reg, val;
  80. char *cmd;
  81. struct pmic *p = &pmic;
  82. /* at least two arguments please */
  83. if (argc < 2)
  84. return cmd_usage(cmdtp);
  85. cmd = argv[1];
  86. if (strcmp(cmd, "dump") == 0) {
  87. pmic_dump(p);
  88. return 0;
  89. }
  90. if (strcmp(cmd, "read") == 0) {
  91. if (argc < 3)
  92. return cmd_usage(cmdtp);
  93. reg = simple_strtoul(argv[2], NULL, 16);
  94. ret = pmic_reg_read(p, reg, &val);
  95. if (ret)
  96. puts("PMIC: Register read failed\n");
  97. printf("\n0x%02x: 0x%08x\n", reg, val);
  98. return 0;
  99. }
  100. if (strcmp(cmd, "write") == 0) {
  101. if (argc < 4)
  102. return cmd_usage(cmdtp);
  103. reg = simple_strtoul(argv[2], NULL, 16);
  104. val = simple_strtoul(argv[3], NULL, 16);
  105. pmic_reg_write(p, reg, val);
  106. return 0;
  107. }
  108. /* No subcommand found */
  109. return 1;
  110. }
  111. U_BOOT_CMD(
  112. pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
  113. "PMIC",
  114. "dump - dump PMIC registers\n"
  115. "pmic read <reg> - read register\n"
  116. "pmic write <reg> <value> - write register"
  117. );