pca9552.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * PCA9552 I2C LED blinker
  3. *
  4. * https://www.nxp.com/docs/en/application-note/AN264.pdf
  5. *
  6. * Copyright (c) 2017-2018, IBM Corporation.
  7. * Copyright (c) 2020 Philippe Mathieu-Daudé
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or
  10. * later. See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qemu/log.h"
  14. #include "qemu/module.h"
  15. #include "qemu/bitops.h"
  16. #include "hw/qdev-properties.h"
  17. #include "hw/misc/pca9552.h"
  18. #include "hw/misc/pca9552_regs.h"
  19. #include "hw/irq.h"
  20. #include "migration/vmstate.h"
  21. #include "qapi/error.h"
  22. #include "qapi/visitor.h"
  23. #include "trace.h"
  24. #include "qom/object.h"
  25. struct PCA955xClass {
  26. /*< private >*/
  27. I2CSlaveClass parent_class;
  28. /*< public >*/
  29. uint8_t pin_count;
  30. uint8_t max_reg;
  31. };
  32. typedef struct PCA955xClass PCA955xClass;
  33. DECLARE_CLASS_CHECKERS(PCA955xClass, PCA955X,
  34. TYPE_PCA955X)
  35. #define PCA9552_LED_ON 0x0
  36. #define PCA9552_LED_OFF 0x1
  37. #define PCA9552_LED_PWM0 0x2
  38. #define PCA9552_LED_PWM1 0x3
  39. static const char *led_state[] = {"on", "off", "pwm0", "pwm1"};
  40. static uint8_t pca955x_pin_get_config(PCA955xState *s, int pin)
  41. {
  42. uint8_t reg = PCA9552_LS0 + (pin / 4);
  43. uint8_t shift = (pin % 4) << 1;
  44. return extract32(s->regs[reg], shift, 2);
  45. }
  46. /* Return INPUT status (bit #N belongs to GPIO #N) */
  47. static uint16_t pca955x_pins_get_status(PCA955xState *s)
  48. {
  49. return (s->regs[PCA9552_INPUT1] << 8) | s->regs[PCA9552_INPUT0];
  50. }
  51. static void pca955x_display_pins_status(PCA955xState *s,
  52. uint16_t previous_pins_status)
  53. {
  54. PCA955xClass *k = PCA955X_GET_CLASS(s);
  55. uint16_t pins_status, pins_changed;
  56. int i;
  57. pins_status = pca955x_pins_get_status(s);
  58. pins_changed = previous_pins_status ^ pins_status;
  59. if (!pins_changed) {
  60. return;
  61. }
  62. if (trace_event_get_state_backends(TRACE_PCA955X_GPIO_STATUS)) {
  63. char *buf = g_newa(char, k->pin_count + 1);
  64. for (i = 0; i < k->pin_count; i++) {
  65. if (extract32(pins_status, i, 1)) {
  66. buf[i] = '*';
  67. } else {
  68. buf[i] = '.';
  69. }
  70. }
  71. buf[i] = '\0';
  72. trace_pca955x_gpio_status(s->description, buf);
  73. }
  74. if (trace_event_get_state_backends(TRACE_PCA955X_GPIO_CHANGE)) {
  75. for (i = 0; i < k->pin_count; i++) {
  76. if (extract32(pins_changed, i, 1)) {
  77. unsigned new_state = extract32(pins_status, i, 1);
  78. /*
  79. * We display the state using the PCA logic ("active-high").
  80. * This is not the state of the LED, which signal might be
  81. * wired "active-low" on the board.
  82. */
  83. trace_pca955x_gpio_change(s->description, i,
  84. !new_state, new_state);
  85. }
  86. }
  87. }
  88. }
  89. static void pca955x_update_pin_input(PCA955xState *s)
  90. {
  91. PCA955xClass *k = PCA955X_GET_CLASS(s);
  92. int i;
  93. for (i = 0; i < k->pin_count; i++) {
  94. uint8_t input_reg = PCA9552_INPUT0 + (i / 8);
  95. uint8_t input_shift = (i % 8);
  96. uint8_t config = pca955x_pin_get_config(s, i);
  97. switch (config) {
  98. case PCA9552_LED_ON:
  99. qemu_set_irq(s->gpio[i], 1);
  100. s->regs[input_reg] |= 1 << input_shift;
  101. break;
  102. case PCA9552_LED_OFF:
  103. qemu_set_irq(s->gpio[i], 0);
  104. s->regs[input_reg] &= ~(1 << input_shift);
  105. break;
  106. case PCA9552_LED_PWM0:
  107. case PCA9552_LED_PWM1:
  108. /* TODO */
  109. default:
  110. break;
  111. }
  112. }
  113. }
  114. static uint8_t pca955x_read(PCA955xState *s, uint8_t reg)
  115. {
  116. switch (reg) {
  117. case PCA9552_INPUT0:
  118. case PCA9552_INPUT1:
  119. case PCA9552_PSC0:
  120. case PCA9552_PWM0:
  121. case PCA9552_PSC1:
  122. case PCA9552_PWM1:
  123. case PCA9552_LS0:
  124. case PCA9552_LS1:
  125. case PCA9552_LS2:
  126. case PCA9552_LS3:
  127. return s->regs[reg];
  128. default:
  129. qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected read to register %d\n",
  130. __func__, reg);
  131. return 0xFF;
  132. }
  133. }
  134. static void pca955x_write(PCA955xState *s, uint8_t reg, uint8_t data)
  135. {
  136. uint16_t pins_status;
  137. switch (reg) {
  138. case PCA9552_PSC0:
  139. case PCA9552_PWM0:
  140. case PCA9552_PSC1:
  141. case PCA9552_PWM1:
  142. s->regs[reg] = data;
  143. break;
  144. case PCA9552_LS0:
  145. case PCA9552_LS1:
  146. case PCA9552_LS2:
  147. case PCA9552_LS3:
  148. pins_status = pca955x_pins_get_status(s);
  149. s->regs[reg] = data;
  150. pca955x_update_pin_input(s);
  151. pca955x_display_pins_status(s, pins_status);
  152. break;
  153. case PCA9552_INPUT0:
  154. case PCA9552_INPUT1:
  155. default:
  156. qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected write to register %d\n",
  157. __func__, reg);
  158. }
  159. }
  160. /*
  161. * When Auto-Increment is on, the register address is incremented
  162. * after each byte is sent to or received by the device. The index
  163. * rollovers to 0 when the maximum register address is reached.
  164. */
  165. static void pca955x_autoinc(PCA955xState *s)
  166. {
  167. PCA955xClass *k = PCA955X_GET_CLASS(s);
  168. if (s->pointer != 0xFF && s->pointer & PCA9552_AUTOINC) {
  169. uint8_t reg = s->pointer & 0xf;
  170. reg = (reg + 1) % (k->max_reg + 1);
  171. s->pointer = reg | PCA9552_AUTOINC;
  172. }
  173. }
  174. static uint8_t pca955x_recv(I2CSlave *i2c)
  175. {
  176. PCA955xState *s = PCA955X(i2c);
  177. uint8_t ret;
  178. ret = pca955x_read(s, s->pointer & 0xf);
  179. /*
  180. * From the Specs:
  181. *
  182. * Important Note: When a Read sequence is initiated and the
  183. * AI bit is set to Logic Level 1, the Read Sequence MUST
  184. * start by a register different from 0.
  185. *
  186. * I don't know what should be done in this case, so throw an
  187. * error.
  188. */
  189. if (s->pointer == PCA9552_AUTOINC) {
  190. qemu_log_mask(LOG_GUEST_ERROR,
  191. "%s: Autoincrement read starting with register 0\n",
  192. __func__);
  193. }
  194. pca955x_autoinc(s);
  195. return ret;
  196. }
  197. static int pca955x_send(I2CSlave *i2c, uint8_t data)
  198. {
  199. PCA955xState *s = PCA955X(i2c);
  200. /* First byte sent by is the register address */
  201. if (s->len == 0) {
  202. s->pointer = data;
  203. s->len++;
  204. } else {
  205. pca955x_write(s, s->pointer & 0xf, data);
  206. pca955x_autoinc(s);
  207. }
  208. return 0;
  209. }
  210. static int pca955x_event(I2CSlave *i2c, enum i2c_event event)
  211. {
  212. PCA955xState *s = PCA955X(i2c);
  213. s->len = 0;
  214. return 0;
  215. }
  216. static void pca955x_get_led(Object *obj, Visitor *v, const char *name,
  217. void *opaque, Error **errp)
  218. {
  219. PCA955xClass *k = PCA955X_GET_CLASS(obj);
  220. PCA955xState *s = PCA955X(obj);
  221. int led, rc, reg;
  222. uint8_t state;
  223. rc = sscanf(name, "led%2d", &led);
  224. if (rc != 1) {
  225. error_setg(errp, "%s: error reading %s", __func__, name);
  226. return;
  227. }
  228. if (led < 0 || led > k->pin_count) {
  229. error_setg(errp, "%s invalid led %s", __func__, name);
  230. return;
  231. }
  232. /*
  233. * Get the LSx register as the qom interface should expose the device
  234. * state, not the modeled 'input line' behaviour which would come from
  235. * reading the INPUTx reg
  236. */
  237. reg = PCA9552_LS0 + led / 4;
  238. state = (pca955x_read(s, reg) >> ((led % 4) * 2)) & 0x3;
  239. visit_type_str(v, name, (char **)&led_state[state], errp);
  240. }
  241. /*
  242. * Return an LED selector register value based on an existing one, with
  243. * the appropriate 2-bit state value set for the given LED number (0-3).
  244. */
  245. static inline uint8_t pca955x_ledsel(uint8_t oldval, int led_num, int state)
  246. {
  247. return (oldval & (~(0x3 << (led_num << 1)))) |
  248. ((state & 0x3) << (led_num << 1));
  249. }
  250. static void pca955x_set_led(Object *obj, Visitor *v, const char *name,
  251. void *opaque, Error **errp)
  252. {
  253. PCA955xClass *k = PCA955X_GET_CLASS(obj);
  254. PCA955xState *s = PCA955X(obj);
  255. int led, rc, reg, val;
  256. uint8_t state;
  257. char *state_str;
  258. if (!visit_type_str(v, name, &state_str, errp)) {
  259. return;
  260. }
  261. rc = sscanf(name, "led%2d", &led);
  262. if (rc != 1) {
  263. error_setg(errp, "%s: error reading %s", __func__, name);
  264. return;
  265. }
  266. if (led < 0 || led > k->pin_count) {
  267. error_setg(errp, "%s invalid led %s", __func__, name);
  268. return;
  269. }
  270. for (state = 0; state < ARRAY_SIZE(led_state); state++) {
  271. if (!strcmp(state_str, led_state[state])) {
  272. break;
  273. }
  274. }
  275. if (state >= ARRAY_SIZE(led_state)) {
  276. error_setg(errp, "%s invalid led state %s", __func__, state_str);
  277. return;
  278. }
  279. reg = PCA9552_LS0 + led / 4;
  280. val = pca955x_read(s, reg);
  281. val = pca955x_ledsel(val, led % 4, state);
  282. pca955x_write(s, reg, val);
  283. }
  284. static const VMStateDescription pca9552_vmstate = {
  285. .name = "PCA9552",
  286. .version_id = 0,
  287. .minimum_version_id = 0,
  288. .fields = (VMStateField[]) {
  289. VMSTATE_UINT8(len, PCA955xState),
  290. VMSTATE_UINT8(pointer, PCA955xState),
  291. VMSTATE_UINT8_ARRAY(regs, PCA955xState, PCA955X_NR_REGS),
  292. VMSTATE_I2C_SLAVE(i2c, PCA955xState),
  293. VMSTATE_END_OF_LIST()
  294. }
  295. };
  296. static void pca9552_reset(DeviceState *dev)
  297. {
  298. PCA955xState *s = PCA955X(dev);
  299. s->regs[PCA9552_PSC0] = 0xFF;
  300. s->regs[PCA9552_PWM0] = 0x80;
  301. s->regs[PCA9552_PSC1] = 0xFF;
  302. s->regs[PCA9552_PWM1] = 0x80;
  303. s->regs[PCA9552_LS0] = 0x55; /* all OFF */
  304. s->regs[PCA9552_LS1] = 0x55;
  305. s->regs[PCA9552_LS2] = 0x55;
  306. s->regs[PCA9552_LS3] = 0x55;
  307. pca955x_update_pin_input(s);
  308. s->pointer = 0xFF;
  309. s->len = 0;
  310. }
  311. static void pca955x_initfn(Object *obj)
  312. {
  313. PCA955xClass *k = PCA955X_GET_CLASS(obj);
  314. int led;
  315. assert(k->pin_count <= PCA955X_PIN_COUNT_MAX);
  316. for (led = 0; led < k->pin_count; led++) {
  317. char *name;
  318. name = g_strdup_printf("led%d", led);
  319. object_property_add(obj, name, "bool", pca955x_get_led, pca955x_set_led,
  320. NULL, NULL);
  321. g_free(name);
  322. }
  323. }
  324. static void pca955x_realize(DeviceState *dev, Error **errp)
  325. {
  326. PCA955xClass *k = PCA955X_GET_CLASS(dev);
  327. PCA955xState *s = PCA955X(dev);
  328. if (!s->description) {
  329. s->description = g_strdup("pca-unspecified");
  330. }
  331. qdev_init_gpio_out(dev, s->gpio, k->pin_count);
  332. }
  333. static Property pca955x_properties[] = {
  334. DEFINE_PROP_STRING("description", PCA955xState, description),
  335. DEFINE_PROP_END_OF_LIST(),
  336. };
  337. static void pca955x_class_init(ObjectClass *klass, void *data)
  338. {
  339. DeviceClass *dc = DEVICE_CLASS(klass);
  340. I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
  341. k->event = pca955x_event;
  342. k->recv = pca955x_recv;
  343. k->send = pca955x_send;
  344. dc->realize = pca955x_realize;
  345. device_class_set_props(dc, pca955x_properties);
  346. }
  347. static const TypeInfo pca955x_info = {
  348. .name = TYPE_PCA955X,
  349. .parent = TYPE_I2C_SLAVE,
  350. .instance_init = pca955x_initfn,
  351. .instance_size = sizeof(PCA955xState),
  352. .class_init = pca955x_class_init,
  353. .class_size = sizeof(PCA955xClass),
  354. .abstract = true,
  355. };
  356. static void pca9552_class_init(ObjectClass *oc, void *data)
  357. {
  358. DeviceClass *dc = DEVICE_CLASS(oc);
  359. PCA955xClass *pc = PCA955X_CLASS(oc);
  360. dc->reset = pca9552_reset;
  361. dc->vmsd = &pca9552_vmstate;
  362. pc->max_reg = PCA9552_LS3;
  363. pc->pin_count = 16;
  364. }
  365. static const TypeInfo pca9552_info = {
  366. .name = TYPE_PCA9552,
  367. .parent = TYPE_PCA955X,
  368. .class_init = pca9552_class_init,
  369. };
  370. static void pca955x_register_types(void)
  371. {
  372. type_register_static(&pca955x_info);
  373. type_register_static(&pca9552_info);
  374. }
  375. type_init(pca955x_register_types)