Lines Matching +full:sdb +full:- +full:gpios
1 // SPDX-License-Identifier: GPL-2.0-only
11 * - maybe add timeout to commands ?
12 * - blocking version of time functions
13 * - polling version of i2c commands (including timer that works with
15 * - maybe avoid some data copies with i2c by directly using the smu cmd
17 * - understand SMU -> CPU events and implement reception of them via
116 if (list_empty(&smu->cmd_list)) in smu_start_cmd()
120 cmd = list_entry(smu->cmd_list.next, struct smu_cmd, link); in smu_start_cmd()
121 smu->cmd_cur = cmd; in smu_start_cmd()
122 list_del(&cmd->link); in smu_start_cmd()
124 DPRINTK("SMU: starting cmd %x, %d bytes data\n", cmd->cmd, in smu_start_cmd()
125 cmd->data_len); in smu_start_cmd()
126 DPRINTK("SMU: data buffer: %8ph\n", cmd->data_buf); in smu_start_cmd()
129 smu->cmd_buf->cmd = cmd->cmd; in smu_start_cmd()
130 smu->cmd_buf->length = cmd->data_len; in smu_start_cmd()
131 memcpy(smu->cmd_buf->data, cmd->data_buf, cmd->data_len); in smu_start_cmd()
134 faddr = (unsigned long)smu->cmd_buf; in smu_start_cmd()
135 fend = faddr + smu->cmd_buf->length + 2; in smu_start_cmd()
148 if (smu->broken_nap) in smu_start_cmd()
155 writel(smu->cmd_buf_abs, smu->db_buf); in smu_start_cmd()
158 pmac_do_feature_call(PMAC_FTR_WRITE_GPIO, NULL, smu->doorbell, 4); in smu_start_cmd()
174 spin_lock_irqsave(&smu->lock, flags); in smu_db_intr()
176 gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell); in smu_db_intr()
178 spin_unlock_irqrestore(&smu->lock, flags); in smu_db_intr()
182 cmd = smu->cmd_cur; in smu_db_intr()
183 smu->cmd_cur = NULL; in smu_db_intr()
197 faddr = (unsigned long)smu->cmd_buf; in smu_db_intr()
201 ack = (~cmd->cmd) & 0xff; in smu_db_intr()
202 if (ack != smu->cmd_buf->cmd) { in smu_db_intr()
204 ack, smu->cmd_buf->cmd); in smu_db_intr()
205 rc = -EIO; in smu_db_intr()
207 reply_len = rc == 0 ? smu->cmd_buf->length : 0; in smu_db_intr()
209 if (reply_len > cmd->reply_len) { in smu_db_intr()
212 reply_len, cmd->reply_len); in smu_db_intr()
213 reply_len = cmd->reply_len; in smu_db_intr()
215 cmd->reply_len = reply_len; in smu_db_intr()
216 if (cmd->reply_buf && reply_len) in smu_db_intr()
217 memcpy(cmd->reply_buf, smu->cmd_buf->data, reply_len); in smu_db_intr()
221 * ownership of the command structure as soon as it's no longer -1 in smu_db_intr()
223 done = cmd->done; in smu_db_intr()
224 misc = cmd->misc; in smu_db_intr()
226 cmd->status = rc; in smu_db_intr()
228 /* Re-enable NAP mode */ in smu_db_intr()
229 if (smu->broken_nap) in smu_db_intr()
234 spin_unlock_irqrestore(&smu->lock, flags); in smu_db_intr()
269 return -ENODEV; in smu_queue_cmd()
270 if (cmd->data_len > SMU_MAX_DATA || in smu_queue_cmd()
271 cmd->reply_len > SMU_MAX_DATA) in smu_queue_cmd()
272 return -EINVAL; in smu_queue_cmd()
274 cmd->status = 1; in smu_queue_cmd()
275 spin_lock_irqsave(&smu->lock, flags); in smu_queue_cmd()
276 list_add_tail(&cmd->link, &smu->cmd_list); in smu_queue_cmd()
277 if (smu->cmd_cur == NULL) in smu_queue_cmd()
279 spin_unlock_irqrestore(&smu->lock, flags); in smu_queue_cmd()
282 if (!smu_irq_inited || !smu->db_irq) in smu_queue_cmd()
295 struct smu_cmd *cmd = &scmd->cmd; in smu_queue_simple()
299 if (data_len > sizeof(scmd->buffer)) in smu_queue_simple()
300 return -EINVAL; in smu_queue_simple()
303 cmd->cmd = command; in smu_queue_simple()
304 cmd->data_len = data_len; in smu_queue_simple()
305 cmd->data_buf = scmd->buffer; in smu_queue_simple()
306 cmd->reply_len = sizeof(scmd->buffer); in smu_queue_simple()
307 cmd->reply_buf = scmd->buffer; in smu_queue_simple()
308 cmd->done = done; in smu_queue_simple()
309 cmd->misc = misc; in smu_queue_simple()
313 scmd->buffer[i] = (u8)va_arg(list, int); in smu_queue_simple()
328 gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell); in smu_poll()
330 smu_db_intr(smu->db_irq, smu); in smu_poll()
346 while(cmd->status == 1) in smu_spinwait_cmd()
368 cmd_buf->cmd = 0x8e; in smu_fill_set_rtc_cmd()
369 cmd_buf->length = 8; in smu_fill_set_rtc_cmd()
370 cmd_buf->data[0] = 0x80; in smu_fill_set_rtc_cmd()
371 cmd_buf->data[1] = hex2bcd(time->tm_sec); in smu_fill_set_rtc_cmd()
372 cmd_buf->data[2] = hex2bcd(time->tm_min); in smu_fill_set_rtc_cmd()
373 cmd_buf->data[3] = hex2bcd(time->tm_hour); in smu_fill_set_rtc_cmd()
374 cmd_buf->data[4] = time->tm_wday; in smu_fill_set_rtc_cmd()
375 cmd_buf->data[5] = hex2bcd(time->tm_mday); in smu_fill_set_rtc_cmd()
376 cmd_buf->data[6] = hex2bcd(time->tm_mon) + 1; in smu_fill_set_rtc_cmd()
377 cmd_buf->data[7] = hex2bcd(time->tm_year - 100); in smu_fill_set_rtc_cmd()
387 return -ENODEV; in smu_get_rtc_time()
396 time->tm_sec = bcd2hex(cmd.buffer[0]); in smu_get_rtc_time()
397 time->tm_min = bcd2hex(cmd.buffer[1]); in smu_get_rtc_time()
398 time->tm_hour = bcd2hex(cmd.buffer[2]); in smu_get_rtc_time()
399 time->tm_wday = bcd2hex(cmd.buffer[3]); in smu_get_rtc_time()
400 time->tm_mday = bcd2hex(cmd.buffer[4]); in smu_get_rtc_time()
401 time->tm_mon = bcd2hex(cmd.buffer[5]) - 1; in smu_get_rtc_time()
402 time->tm_year = bcd2hex(cmd.buffer[6]) + 100; in smu_get_rtc_time()
414 return -ENODEV; in smu_set_rtc_time()
418 hex2bcd(time->tm_sec), in smu_set_rtc_time()
419 hex2bcd(time->tm_min), in smu_set_rtc_time()
420 hex2bcd(time->tm_hour), in smu_set_rtc_time()
421 time->tm_wday, in smu_set_rtc_time()
422 hex2bcd(time->tm_mday), in smu_set_rtc_time()
423 hex2bcd(time->tm_mon) + 1, in smu_set_rtc_time()
424 hex2bcd(time->tm_year - 100)); in smu_set_rtc_time()
480 return -ENODEV; in smu_init()
491 ret = -EINVAL; in smu_init()
500 spin_lock_init(&smu->lock); in smu_init()
501 INIT_LIST_HEAD(&smu->cmd_list); in smu_init()
502 INIT_LIST_HEAD(&smu->cmd_i2c_list); in smu_init()
503 smu->of_node = np; in smu_init()
504 smu->db_irq = 0; in smu_init()
505 smu->msg_irq = 0; in smu_init()
510 smu->cmd_buf_abs = (u32)smu_cmdbuf_abs; in smu_init()
511 smu->cmd_buf = __va(smu_cmdbuf_abs); in smu_init()
513 smu->db_node = of_find_node_by_name(NULL, "smu-doorbell"); in smu_init()
514 if (smu->db_node == NULL) { in smu_init()
516 ret = -ENXIO; in smu_init()
519 if (of_property_read_reg(smu->db_node, 0, &data, NULL)) { in smu_init()
521 ret = -ENXIO; in smu_init()
526 * and ack. GPIOs are at 0x50, best would be to find that out in smu_init()
527 * in the device-tree though. in smu_init()
529 smu->doorbell = data; in smu_init()
530 if (smu->doorbell < 0x50) in smu_init()
531 smu->doorbell += 0x50; in smu_init()
533 /* Now look for the smu-interrupt GPIO */ in smu_init()
535 smu->msg_node = of_find_node_by_name(NULL, "smu-interrupt"); in smu_init()
536 if (smu->msg_node == NULL) in smu_init()
538 if (of_property_read_reg(smu->msg_node, 0, &data, NULL)) { in smu_init()
539 of_node_put(smu->msg_node); in smu_init()
540 smu->msg_node = NULL; in smu_init()
543 smu->msg = data; in smu_init()
544 if (smu->msg < 0x50) in smu_init()
545 smu->msg += 0x50; in smu_init()
548 /* Doorbell buffer is currently hard-coded, I didn't find a proper in smu_init()
549 * device-tree entry giving the address. Best would probably to use in smu_init()
552 smu->db_buf = ioremap(0x8000860c, 0x1000); in smu_init()
553 if (smu->db_buf == NULL) { in smu_init()
555 ret = -ENXIO; in smu_init()
560 smu->broken_nap = pmac_get_uninorth_variant() < 4; in smu_init()
561 if (smu->broken_nap) in smu_init()
568 of_node_put(smu->msg_node); in smu_init()
570 of_node_put(smu->db_node); in smu_init()
585 timer_setup(&smu->i2c_timer, smu_i2c_retry, 0); in smu_late_init()
587 if (smu->db_node) { in smu_late_init()
588 smu->db_irq = irq_of_parse_and_map(smu->db_node, 0); in smu_late_init()
589 if (!smu->db_irq) in smu_late_init()
591 smu->db_node); in smu_late_init()
593 if (smu->msg_node) { in smu_late_init()
594 smu->msg_irq = irq_of_parse_and_map(smu->msg_node, 0); in smu_late_init()
595 if (!smu->msg_irq) in smu_late_init()
597 smu->msg_node); in smu_late_init()
604 if (smu->db_irq) { in smu_late_init()
605 if (request_irq(smu->db_irq, smu_db_intr, in smu_late_init()
609 smu->db_irq); in smu_late_init()
610 smu->db_irq = 0; in smu_late_init()
614 if (smu->msg_irq) { in smu_late_init()
615 if (request_irq(smu->msg_irq, smu_msg_intr, in smu_late_init()
619 smu->msg_irq); in smu_late_init()
620 smu->msg_irq = 0; in smu_late_init()
640 for_each_child_of_node(smu->of_node, np) in smu_expose_childs()
641 if (of_device_is_compatible(np, "smu-sensors")) in smu_expose_childs()
642 of_platform_device_create(np, "smu-sensors", in smu_expose_childs()
643 &smu->of_dev->dev); in smu_expose_childs()
651 return -ENODEV; in smu_platform_probe()
652 smu->of_dev = dev; in smu_platform_probe()
697 return smu->of_dev; in smu_get_ofdev()
708 void (*done)(struct smu_i2c_cmd *cmd, void *misc) = cmd->done; in smu_i2c_complete_command()
709 void *misc = cmd->misc; in smu_i2c_complete_command()
713 if (!fail && cmd->read) { in smu_i2c_complete_command()
714 if (cmd->pdata[0] < 1) in smu_i2c_complete_command()
717 memcpy(cmd->info.data, &cmd->pdata[1], in smu_i2c_complete_command()
718 cmd->info.datalen); in smu_i2c_complete_command()
727 spin_lock_irqsave(&smu->lock, flags); in smu_i2c_complete_command()
728 smu->cmd_i2c_cur = NULL; in smu_i2c_complete_command()
730 cmd->status = fail ? -EIO : 0; in smu_i2c_complete_command()
733 if (!list_empty(&smu->cmd_i2c_list)) { in smu_i2c_complete_command()
737 newcmd = list_entry(smu->cmd_i2c_list.next, in smu_i2c_complete_command()
739 smu->cmd_i2c_cur = newcmd; in smu_i2c_complete_command()
740 list_del(&cmd->link); in smu_i2c_complete_command()
743 list_add_tail(&cmd->scmd.link, &smu->cmd_list); in smu_i2c_complete_command()
744 if (smu->cmd_cur == NULL) in smu_i2c_complete_command()
747 spin_unlock_irqrestore(&smu->lock, flags); in smu_i2c_complete_command()
758 struct smu_i2c_cmd *cmd = smu->cmd_i2c_cur; in smu_i2c_retry()
763 cmd->pdata[0] = 0xff; in smu_i2c_retry()
764 cmd->scmd.reply_len = sizeof(cmd->pdata); in smu_i2c_retry()
765 smu_queue_cmd(&cmd->scmd); in smu_i2c_retry()
775 cmd->stage, scmd->status, cmd->pdata[0], scmd->reply_len); in smu_i2c_low_completion()
778 if (scmd->status < 0) in smu_i2c_low_completion()
780 else if (cmd->read) { in smu_i2c_low_completion()
781 if (cmd->stage == 0) in smu_i2c_low_completion()
782 fail = cmd->pdata[0] != 0; in smu_i2c_low_completion()
784 fail = cmd->pdata[0] >= 0x80; in smu_i2c_low_completion()
786 fail = cmd->pdata[0] != 0; in smu_i2c_low_completion()
791 if (fail && --cmd->retries > 0) { in smu_i2c_low_completion()
793 BUG_ON(cmd != smu->cmd_i2c_cur); in smu_i2c_low_completion()
799 mod_timer(&smu->i2c_timer, jiffies + msecs_to_jiffies(5)); in smu_i2c_low_completion()
804 if (fail || cmd->stage != 0) { in smu_i2c_low_completion()
812 scmd->reply_buf = cmd->pdata; in smu_i2c_low_completion()
813 scmd->reply_len = sizeof(cmd->pdata); in smu_i2c_low_completion()
814 scmd->data_buf = cmd->pdata; in smu_i2c_low_completion()
815 scmd->data_len = 1; in smu_i2c_low_completion()
816 cmd->pdata[0] = 0; in smu_i2c_low_completion()
817 cmd->stage = 1; in smu_i2c_low_completion()
818 cmd->retries = 20; in smu_i2c_low_completion()
828 return -ENODEV; in smu_queue_i2c()
831 cmd->scmd.cmd = SMU_CMD_I2C_COMMAND; in smu_queue_i2c()
832 cmd->scmd.done = smu_i2c_low_completion; in smu_queue_i2c()
833 cmd->scmd.misc = cmd; in smu_queue_i2c()
834 cmd->scmd.reply_buf = cmd->pdata; in smu_queue_i2c()
835 cmd->scmd.reply_len = sizeof(cmd->pdata); in smu_queue_i2c()
836 cmd->scmd.data_buf = (u8 *)(char *)&cmd->info; in smu_queue_i2c()
837 cmd->scmd.status = 1; in smu_queue_i2c()
838 cmd->stage = 0; in smu_queue_i2c()
839 cmd->pdata[0] = 0xff; in smu_queue_i2c()
840 cmd->retries = 20; in smu_queue_i2c()
841 cmd->status = 1; in smu_queue_i2c()
846 cmd->info.caddr = cmd->info.devaddr; in smu_queue_i2c()
847 cmd->read = cmd->info.devaddr & 0x01; in smu_queue_i2c()
848 switch(cmd->info.type) { in smu_queue_i2c()
850 cmd->info.sublen = 0; in smu_queue_i2c()
851 memset(cmd->info.subaddr, 0, sizeof(cmd->info.subaddr)); in smu_queue_i2c()
854 cmd->info.devaddr &= 0xfe; in smu_queue_i2c()
857 if (cmd->info.sublen > 3) in smu_queue_i2c()
858 return -EINVAL; in smu_queue_i2c()
861 return -EINVAL; in smu_queue_i2c()
866 if (cmd->read) { in smu_queue_i2c()
867 if (cmd->info.datalen > SMU_I2C_READ_MAX) in smu_queue_i2c()
868 return -EINVAL; in smu_queue_i2c()
869 memset(cmd->info.data, 0xff, cmd->info.datalen); in smu_queue_i2c()
870 cmd->scmd.data_len = 9; in smu_queue_i2c()
872 if (cmd->info.datalen > SMU_I2C_WRITE_MAX) in smu_queue_i2c()
873 return -EINVAL; in smu_queue_i2c()
874 cmd->scmd.data_len = 9 + cmd->info.datalen; in smu_queue_i2c()
879 cmd->read ? "read" : "write", cmd->info.datalen, in smu_queue_i2c()
880 cmd->info.bus, cmd->info.caddr, in smu_queue_i2c()
881 cmd->info.subaddr[0], cmd->info.type); in smu_queue_i2c()
887 spin_lock_irqsave(&smu->lock, flags); in smu_queue_i2c()
888 if (smu->cmd_i2c_cur == NULL) { in smu_queue_i2c()
889 smu->cmd_i2c_cur = cmd; in smu_queue_i2c()
890 list_add_tail(&cmd->scmd.link, &smu->cmd_list); in smu_queue_i2c()
891 if (smu->cmd_cur == NULL) in smu_queue_i2c()
894 list_add_tail(&cmd->link, &smu->cmd_i2c_list); in smu_queue_i2c()
895 spin_unlock_irqrestore(&smu->lock, flags); in smu_queue_i2c()
942 return -EIO; in smu_read_datablock()
944 len -= clen; in smu_read_datablock()
960 DPRINTK("SMU: Query partition infos ... (irq=%d)\n", smu->db_irq); in smu_create_sdb_partition()
976 * for "sdb-partition-XX" that we append at the end of the buffer in smu_create_sdb_partition()
984 prop->name = ((char *)prop) + tlen - 18; in smu_create_sdb_partition()
985 sprintf(prop->name, "sdb-partition-%02x", id); in smu_create_sdb_partition()
986 prop->length = len; in smu_create_sdb_partition()
987 prop->value = hdr; in smu_create_sdb_partition()
988 prop->next = NULL; in smu_create_sdb_partition()
998 if (hdr->id != id) { in smu_create_sdb_partition()
1000 "%02x !\n", id, hdr->id); in smu_create_sdb_partition()
1003 if (of_add_property(smu->of_node, prop)) { in smu_create_sdb_partition()
1004 printk(KERN_DEBUG "SMU: Failed creating sdb-partition-%02x " in smu_create_sdb_partition()
1027 sprintf(pname, "sdb-partition-%02x", id); in __smu_get_sdb_partition()
1039 part = of_get_property(smu->of_node, pname, size); in __smu_get_sdb_partition()
1044 *size = part->len << 2; in __smu_get_sdb_partition()
1090 return -ENOMEM; in smu_open()
1091 spin_lock_init(&pp->lock); in smu_open()
1092 pp->mode = smu_file_commands; in smu_open()
1093 init_waitqueue_head(&pp->wait); in smu_open()
1097 list_add(&pp->list, &smu_clist); in smu_open()
1099 file->private_data = pp; in smu_open()
1110 wake_up_all(&pp->wait); in smu_user_cmd_done()
1117 struct smu_private *pp = file->private_data; in smu_write()
1122 if (pp->busy) in smu_write()
1123 return -EBUSY; in smu_write()
1125 return -EFAULT; in smu_write()
1127 pp->mode = smu_file_events; in smu_write()
1133 return -EINVAL; in smu_write()
1138 return -EINVAL; in smu_write()
1139 else if (pp->mode != smu_file_commands) in smu_write()
1140 return -EBADFD; in smu_write()
1142 return -EINVAL; in smu_write()
1144 spin_lock_irqsave(&pp->lock, flags); in smu_write()
1145 if (pp->busy) { in smu_write()
1146 spin_unlock_irqrestore(&pp->lock, flags); in smu_write()
1147 return -EBUSY; in smu_write()
1149 pp->busy = 1; in smu_write()
1150 pp->cmd.status = 1; in smu_write()
1151 spin_unlock_irqrestore(&pp->lock, flags); in smu_write()
1153 if (copy_from_user(pp->buffer, buf + sizeof(hdr), hdr.data_len)) { in smu_write()
1154 pp->busy = 0; in smu_write()
1155 return -EFAULT; in smu_write()
1158 pp->cmd.cmd = hdr.cmd; in smu_write()
1159 pp->cmd.data_len = hdr.data_len; in smu_write()
1160 pp->cmd.reply_len = SMU_MAX_DATA; in smu_write()
1161 pp->cmd.data_buf = pp->buffer; in smu_write()
1162 pp->cmd.reply_buf = pp->buffer; in smu_write()
1163 pp->cmd.done = smu_user_cmd_done; in smu_write()
1164 pp->cmd.misc = pp; in smu_write()
1165 rc = smu_queue_cmd(&pp->cmd); in smu_write()
1180 if (!pp->busy) in smu_read_command()
1183 return -EOVERFLOW; in smu_read_command()
1184 spin_lock_irqsave(&pp->lock, flags); in smu_read_command()
1185 if (pp->cmd.status == 1) { in smu_read_command()
1186 if (file->f_flags & O_NONBLOCK) { in smu_read_command()
1187 spin_unlock_irqrestore(&pp->lock, flags); in smu_read_command()
1188 return -EAGAIN; in smu_read_command()
1190 add_wait_queue(&pp->wait, &wait); in smu_read_command()
1194 if (pp->cmd.status != 1) in smu_read_command()
1196 rc = -ERESTARTSYS; in smu_read_command()
1199 spin_unlock_irqrestore(&pp->lock, flags); in smu_read_command()
1201 spin_lock_irqsave(&pp->lock, flags); in smu_read_command()
1204 remove_wait_queue(&pp->wait, &wait); in smu_read_command()
1206 spin_unlock_irqrestore(&pp->lock, flags); in smu_read_command()
1209 if (pp->cmd.status != 0) in smu_read_command()
1210 pp->cmd.reply_len = 0; in smu_read_command()
1211 size = sizeof(hdr) + pp->cmd.reply_len; in smu_read_command()
1215 hdr.status = pp->cmd.status; in smu_read_command()
1216 hdr.reply_len = pp->cmd.reply_len; in smu_read_command()
1218 return -EFAULT; in smu_read_command()
1219 size -= sizeof(hdr); in smu_read_command()
1220 if (size && copy_to_user(buf + sizeof(hdr), pp->buffer, size)) in smu_read_command()
1221 return -EFAULT; in smu_read_command()
1222 pp->busy = 0; in smu_read_command()
1240 struct smu_private *pp = file->private_data; in smu_read()
1242 if (pp->mode == smu_file_commands) in smu_read()
1244 if (pp->mode == smu_file_events) in smu_read()
1247 return -EBADFD; in smu_read()
1252 struct smu_private *pp = file->private_data; in smu_fpoll()
1259 if (pp->mode == smu_file_commands) { in smu_fpoll()
1260 poll_wait(file, &pp->wait, wait); in smu_fpoll()
1262 spin_lock_irqsave(&pp->lock, flags); in smu_fpoll()
1263 if (pp->busy && pp->cmd.status != 1) in smu_fpoll()
1265 spin_unlock_irqrestore(&pp->lock, flags); in smu_fpoll()
1267 if (pp->mode == smu_file_events) { in smu_fpoll()
1275 struct smu_private *pp = file->private_data; in smu_release()
1282 file->private_data = NULL; in smu_release()
1285 spin_lock_irqsave(&pp->lock, flags); in smu_release()
1286 pp->mode = smu_file_closing; in smu_release()
1287 busy = pp->busy; in smu_release()
1290 if (busy && pp->cmd.status == 1) { in smu_release()
1293 add_wait_queue(&pp->wait, &wait); in smu_release()
1296 if (pp->cmd.status != 1) in smu_release()
1298 spin_unlock_irqrestore(&pp->lock, flags); in smu_release()
1300 spin_lock_irqsave(&pp->lock, flags); in smu_release()
1303 remove_wait_queue(&pp->wait, &wait); in smu_release()
1305 spin_unlock_irqrestore(&pp->lock, flags); in smu_release()
1308 list_del(&pp->list); in smu_release()
1331 return -ENODEV; in smu_device_init()
1333 printk(KERN_ERR "via-pmu: cannot register misc device.\n"); in smu_device_init()