pca9552.c 13 KB

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