Linux kernel about SPI device Tree parameter resolution

Source: Internet
Author: User
Tags goto

  • Recently made an SPI device driver upgrade from board-level device driver to device-tree device driver, which needs to understand the parsing of the SPI Device tree code.
  • The device tree is configured as follows:
    503&spi0 {504Status ="Okay";505Pinctrl-name ="Default";506Pinctrl-0= <&spi0_pins>;507ti,pindir-d0-out-d1-in;508509wk2124a {510Compatible ="wk2124a";//Match string511Reg = <0>;//CS +# Spi-cpha = <1>;//Configure the SPI mode513# Spi-tx-bus-width = <1>;//This is the bus width of the spi-tx514# Spi-rx-bus-width = <1>;515Spi-max-frequency = <10000000>;//SPI max rate configuration516};517};
  • Code tracking
    //Drivers/spi/spi.c2772Postcore_initcall (Spi_init);2733 Static int__init Spi_init (void)2734{2735     intStatus27362737BUF = Kmalloc (Spi_bufsiz, Gfp_kernel);2738     if(!BUF) {2739status =-enomem;2740         GotoErr0;2741}27422743Status = Bus_register (&spi_bus_type);2744     if(Status <0)2745         GotoERR1;27462747Status = Class_register (&spi_master_class);2748     if(Status <0)2749         GotoERR2;27502751     if(is_enabled (config_of_dynamic))2752Warn_on (Of_reconfig_notifier_register (&spi_of_notifier));27532754     return 0;27552756ERR2:2757Bus_unregister (&spi_bus_type);2758ERR1:2759Kfree (BUF);2760BUF = NULL;2761ERR0:2762     returnStatus2763}2726 Static structNotifier_block Spi_of_notifier = {2727. Notifier_call = Of_spi_notify,2728};2686 Static intOf_spi_notify (structNotifier_block *NB,unsigned LongAction2687              void*ARG)2688{2689     structOf_reconfig_data *rd = arg;2690     structSpi_master *master;2691     structSpi_device *spi;26922693     Switch(Of_reconfig_get_state_change (Action, ARG)) {2694      CaseOf_reconfig_change_add:2695master = Of_find_spi_master_by_node (rd->dn->parent);2696         if(Master = = NULL)2697             returnNOTIFY_OK;/* Not for US * /26982699SPI = Of_register_spi_device (master, RD-&GT;DN);//---> Registration equipment2700Put_device (&master->dev);// ... ... 27222723     returnNOTIFY_OK;2724}1428#if defined (config_of)1429 Static structSpi_device *1430Of_register_spi_device (structSpi_master *master,structDevice_node *NC)1431{1432     structSpi_device *spi;1433     intRc1434U32 value;14351436     / * Alloc an spi_device * /1437SPI = Spi_alloc_device (master);1438     if(!SPI) {1439Dev_err (&master->dev,"Spi_device alloc error for%s\ n",1440Nc->full_name);1441rc =-enomem;1442         GotoErr_out;1443}14441445     / * Select Device driver * /1446rc = Of_modalias_node (NC, Spi->modalias,1447                 sizeof(Spi->modalias));1448     if(RC <0) {1449Dev_err (&master->dev,"Cannot find Modalias for%s\ n",1450Nc->full_name);1451         GotoErr_out;1452}14531454     / * Device address * /1455rc = Of_property_read_u32 (NC,"Reg", &value);1456     if(RC) {1457Dev_err (&master->dev,"%s have no valid ' Reg ' property (%d)\ n",1458Nc->full_name, RC);1459         GotoErr_out;1460}1461Spi->chip_select = value;14621463     /* Mode (Clock phase/polarity/etc.) */1464     if(Of_find_property (NC,"Spi-cpha", NULL))1465Spi->mode |= Spi_cpha;1466     if(Of_find_property (NC,"Spi-cpol", NULL))1467Spi->mode |= Spi_cpol;1468     if(Of_find_property (NC,"Spi-cs-high", NULL))1469Spi->mode |= Spi_cs_high;1470     if(Of_find_property (NC,"Spi-3wire", NULL))1471Spi->mode |= Spi_3wire;1472     if(Of_find_property (NC,"Spi-lsb-first", NULL))1473Spi->mode |= Spi_lsb_first;14741475     / * Device dual/quad mode * /1476     if(!of_property_read_u32 (NC,"Spi-tx-bus-width", &value)) {1477         Switch(value) {1478          Case 1:1479              Break;1480          Case 2:1481Spi->mode |= spi_tx_dual;1482              Break;1483          Case 4:1484Spi->mode |= Spi_tx_quad;1485              Break;1486         default:1487Dev_warn (&master->dev,1488                 "Spi-tx-bus-width%d not supported\ n",1489Value);1490              Break;1491}1492}14931494     if(!of_property_read_u32 (NC,"Spi-rx-bus-width", &value)) {1495         Switch(value) {1496          Case 1:1497              Break;1498          Case 2:1499Spi->mode |= spi_rx_dual; the              Break;1501          Case 4:1502Spi->mode |= Spi_rx_quad;1503              Break;1504         default:1505Dev_warn (&master->dev,1506                 "Spi-rx-bus-width%d not supported\ n",1507Value);1508              Break;1509}1510}15111512     / * Device speed * /1513rc = Of_property_read_u32 (NC,"Spi-max-frequency", &value);1514     if(RC) {1515Dev_err (&master->dev,"%s have no valid ' spi-max-frequency ' property (%d)\ n",1516Nc->full_name, RC);1517         GotoErr_out;1518}1519Spi->max_speed_hz = value;15201521     / * Store A pointer to the node in the device structure * /1522Of_node_get (NC);1523Spi->dev.of_node = NC;15241525     / * Register the new device * /1526rc = Spi_add_device (SPI);1527     if(RC) {1528Dev_err (&master->dev,"Spi_device Register error%s\ n",1529Nc->full_name);1530         GotoErr_out;1531}15321533     returnSpi15341535Err_out:1536Spi_dev_put (SPI);1537     returnErr_ptr (RC);1538}

Linux kernel about SPI device Tree parameter resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.