Advertisement
lehjr

nvrm_i2c_dispatch (i2c debugging for camera)

Sep 10th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.82 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2009 NVIDIA Corporation.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  *
  8.  * Redistributions of source code must retain the above copyright notice,
  9.  * this list of conditions and the following disclaimer.
  10.  *
  11.  * Redistributions in binary form must reproduce the above copyright notice,
  12.  * this list of conditions and the following disclaimer in the documentation
  13.  * and/or other materials provided with the distribution.
  14.  *
  15.  * Neither the name of the NVIDIA Corporation nor the names of its contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  23.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29.  * POSSIBILITY OF SUCH DAMAGE.
  30.  *
  31.  */
  32.  
  33. #define NV_IDL_IS_DISPATCH
  34.  
  35. #include <linux/kernel.h> //printk
  36.  
  37. #include "nvcommon.h"
  38. #include "nvos.h"
  39. #include "nvassert.h"
  40. #include "nvreftrack.h"
  41. #include "nvidlcmd.h"
  42. #include "nvrm_i2c.h"
  43.  
  44. #define OFFSET( s, e ) (NvU32)(void *)(&(((s*)0)->e))
  45.  
  46.  
  47. typedef struct NvRmI2cTransaction_in_t
  48. {
  49.     NvU32 package_;             // what?
  50.     NvU32 function_;                // what's this?
  51.     NvRmI2cHandle hI2c;             // address in here?
  52.     NvU32 I2cPinMap;                // hmm, maybe
  53.     NvU32 WaitTimeoutInMilliSeconds;        // ok
  54.     NvU32 ClockSpeedKHz;            // interesting, but won't need it for long
  55.     NvU8  * Data;               // interesting
  56.     NvU32 DataLen;              // sure, ok
  57.     NvRmI2cTransactionInfo  * Transaction;  // what's this?
  58.     NvU32 NumOfTransactions;            // ok, sure. Why not.
  59. } NV_ALIGN(4) NvRmI2cTransaction_in;
  60.  
  61. typedef struct NvRmI2cTransaction_inout_t
  62. {
  63.     NvU32 dummy_;
  64. } NV_ALIGN(4) NvRmI2cTransaction_inout;
  65.  
  66. typedef struct NvRmI2cTransaction_out_t
  67. {
  68.     NvError ret_;
  69. } NV_ALIGN(4) NvRmI2cTransaction_out;
  70.  
  71. typedef struct NvRmI2cTransaction_params_t
  72. {
  73.     NvRmI2cTransaction_in in;
  74.     NvRmI2cTransaction_inout inout;
  75.     NvRmI2cTransaction_out out;
  76. } NvRmI2cTransaction_params;
  77.  
  78. typedef struct NvRmI2cClose_in_t
  79. {
  80.     NvU32 package_;
  81.     NvU32 function_;
  82.     NvRmI2cHandle hI2c;
  83. } NV_ALIGN(4) NvRmI2cClose_in;
  84.  
  85. typedef struct NvRmI2cClose_inout_t
  86. {
  87.     NvU32 dummy_;
  88. } NV_ALIGN(4) NvRmI2cClose_inout;
  89.  
  90. typedef struct NvRmI2cClose_out_t
  91. {
  92.     NvU32 dummy_;
  93. } NV_ALIGN(4) NvRmI2cClose_out;
  94.  
  95. typedef struct NvRmI2cClose_params_t
  96. {
  97.     NvRmI2cClose_in in;
  98.     NvRmI2cClose_inout inout;
  99.     NvRmI2cClose_out out;
  100. } NvRmI2cClose_params;
  101.  
  102. typedef struct NvRmI2cOpen_in_t
  103. {
  104.     NvU32 package_;
  105.     NvU32 function_;
  106.     NvRmDeviceHandle hDevice;
  107.     NvU32 IoModule;
  108.     NvU32 instance;
  109. } NV_ALIGN(4) NvRmI2cOpen_in;
  110.  
  111. typedef struct NvRmI2cOpen_inout_t
  112. {
  113.     NvU32 dummy_;
  114. } NV_ALIGN(4) NvRmI2cOpen_inout;
  115.  
  116. typedef struct NvRmI2cOpen_out_t
  117. {
  118.     NvError ret_;
  119.     NvRmI2cHandle phI2c;
  120. } NV_ALIGN(4) NvRmI2cOpen_out;
  121.  
  122. typedef struct NvRmI2cOpen_params_t
  123. {
  124.     NvRmI2cOpen_in in;
  125.     NvRmI2cOpen_inout inout;
  126.     NvRmI2cOpen_out out;
  127. } NvRmI2cOpen_params;
  128.  
  129. bool flash_read = 0;
  130.  
  131.  
  132. static NvError NvRmI2cTransaction_dispatch_( void *InBuffer, NvU32 InSize, void *OutBuffer, NvU32 OutSize, NvDispatchCtx* Ctx )
  133. {
  134.     NvError err_ = NvSuccess;
  135.     NvRmI2cTransaction_in *p_in;
  136.     NvRmI2cTransaction_out *p_out;
  137.     NvU8  *Data = NULL;
  138.     NvRmI2cTransactionInfo *Transaction = NULL;
  139.  
  140.     bool print_data, print_read;
  141.     int thing;
  142.     NvU32 i2c_address; // the i2c device address
  143.  
  144.     p_in = (NvRmI2cTransaction_in *)InBuffer;
  145.     p_out = (NvRmI2cTransaction_out *)((NvU8 *)OutBuffer + OFFSET(NvRmI2cTransaction_params, out) - OFFSET(NvRmI2cTransaction_params, inout));
  146.  
  147.     i2c_address = p_in->Transaction->Address;
  148.     i2c_address = i2c_address >> 1;
  149.  
  150.     if( p_in->DataLen && p_in->Data )
  151.     {
  152.         Data = (NvU8  *)NvOsAlloc( p_in->DataLen * sizeof( NvU8  ) );
  153.  
  154.         if( !Data )
  155.         {
  156.             err_ = NvError_InsufficientMemory;
  157.             goto clean;
  158.         }
  159.         if( p_in->Data )
  160.         {
  161.             err_ = NvOsCopyIn( Data, p_in->Data, p_in->DataLen * sizeof( NvU8  ) );
  162.             if( err_ != NvSuccess )
  163.             {
  164.                 err_ = NvError_BadParameter;
  165.                 goto clean;
  166.             }
  167.         }
  168.     }
  169.     if( p_in->NumOfTransactions && p_in->Transaction )
  170.     {
  171.         Transaction = (NvRmI2cTransactionInfo  *)NvOsAlloc( p_in->NumOfTransactions * sizeof( NvRmI2cTransactionInfo  ) );
  172.         if( !Transaction )
  173.         {
  174.             err_ = NvError_InsufficientMemory;
  175.             goto clean;
  176.         }
  177.         if( p_in->Transaction )
  178.         {
  179.             err_ = NvOsCopyIn( Transaction, p_in->Transaction, p_in->NumOfTransactions * sizeof( NvRmI2cTransactionInfo  ) );
  180.             if( err_ != NvSuccess )
  181.             {
  182.                 err_ = NvError_BadParameter;
  183.                 goto clean;
  184.             }
  185.         }
  186.     }
  187.  
  188.  
  189.  
  190.  
  191.  
  192.     print_data = 0; // whether or not to print out using the "for" loop
  193.     print_read = 1; // print out read requests and read values?
  194.  
  195.     /* note that these addresses are only for Olympus. Any devices not using the same cameras
  196.      * will likely have different addresses.
  197.      *
  198.      * cat /proc/kmsg | grep Thingie
  199.      */
  200.     switch(i2c_address) {
  201.         /* front camera */
  202.         case 0x3d:
  203.             printk (KERN_INFO "Thingie: front cam: 0x%x",i2c_address);
  204.         print_data = 1;
  205.             break;
  206.  
  207.     /* rear camera */
  208.         case 0x36:
  209.             // The camera regisers are 16 bit addresses
  210.             if (p_in->DataLen == 3){
  211.                 //printk (KERN_INFO "Thingie: i2cset -y 2 0x36 0x%x 0x%x 0x%x i", Data[0], Data[1], Data[2]);
  212.                 // This would be to format for drivers (untested)
  213.         printk (KERN_INFO "Thingie: OV5650  {0x%4x, 0x%2x}", (int)Data[0] << 8 | Data[1], Data[2]);
  214.  
  215.                 print_data = 0;
  216.             }
  217.         // This is for bulk transfers
  218.             else if (p_in->DataLen > 3){
  219.             //printk (KERN_INFO "Thingie rear cam: 0x%x", i2c_address);
  220.                 printk (KERN_INFO "# Thingie: Starting rear camera bulk transfer length: %d", p_in->DataLen);
  221.                 for (thing=0; thing< p_in->DataLen; thing+=3)
  222.                 {
  223.                     //printk (KERN_INFO "Thingie: i2cset -y 2 0x36 0x%x 0x%x 0x%x i", Data[thing], Data[thing + 1], Data[thing + 2]);
  224.             printk (KERN_INFO "Thingie: OV5650  {0x%4x, 0x%2x}", (int)Data[thing] << 8 | Data[thing + 1], Data[thing + 2]);
  225.  
  226.                 }
  227.                 printk (KERN_INFO "# Thingie: Finished bulk transfer");
  228.             print_data = 0;
  229.             }
  230.             // a length of 2 indicates a read transaction
  231.             // a length of 1 will follow the read transaction and is the actual value being read back
  232.             else if ((p_in->DataLen == 2) && print_read){
  233.                 printk (KERN_INFO "# Thingie: Sending read request to rear camera");
  234.                 printk (KERN_INFO "Thingie: i2cset -y 2 0x36 0x%2x 0x%2x", Data[0], Data[1]);
  235.             print_data = 0;
  236.             }
  237.             break;
  238.  
  239.         /* camera flash */
  240.         case 0x53:
  241.             if (p_in->DataLen == 2){
  242.                 printk (KERN_INFO "Thingie: i2cset -y 2 0x53 0x%2x 0x%2x", Data[0], Data[1]);
  243.                 print_data = 0;
  244.             }
  245.             else if (!flash_read) {
  246.                 printk (KERN_INFO "# Thingie: Sending read request to camera flash");
  247.                 printk (KERN_INFO "Thingie: i2cset -y 2 0x53 0x%2x", Data[0]);
  248.             print_data = 1;
  249.                 flash_read = 1;
  250.             }
  251.             break;
  252.  
  253.     /* focuser */
  254.         case 0x0c:
  255.             //printk (KERN_INFO "Thingie focuser: 0x%x",i2c_address);
  256.         print_data = 0; // too spammy right now
  257.             break;
  258.  
  259.     /* only happens during probing on power up */
  260.         default:
  261.             // not much to see here            
  262.             // printk (KERN_INFO "Thingie: probing i2c address: 0x%2x",i2c_address);
  263.         print_data = 0;
  264.             break;
  265.     }
  266.  
  267.     if ((print_data) && (p_in->DataLen > 1)){
  268.         //printk (KERN_INFO "Thingie sizeof (Data): %d",sizeof(Data) );
  269.         printk (KERN_INFO "Thingie: p_in->DataLen: %d",p_in->DataLen );
  270.     //printk (KERN_INFO "Thingie Data: %d",Data);
  271.         //for (thing=0; thing< sizeof(Data); thing++)
  272.         for (thing=0; thing< p_in->DataLen; thing++)
  273.         {
  274.             printk (KERN_INFO "Thingie: Data[%d]: 0x%2x",thing, Data[thing]);
  275.         }
  276.     }
  277.  
  278.     // this is where the transaction actually takes place  
  279.     p_out->ret_ = NvRmI2cTransaction( p_in->hI2c, p_in->I2cPinMap, p_in->WaitTimeoutInMilliSeconds, p_in->ClockSpeedKHz, Data, p_in->DataLen, Transaction, p_in->NumOfTransactions );
  280.    
  281.     if ((p_in->DataLen == 1) && (print_read || flash_read)){
  282.         printk (KERN_INFO "# Thingie: read value 0x%2x", Data[0]);
  283.         printk (KERN_INFO "#                   Thingie: Finished read request");
  284.     flash_read = 0;
  285.     }
  286.  
  287.  
  288.     if(p_in->Data && Data)
  289.     {
  290.         err_ = NvOsCopyOut( p_in->Data, Data, p_in->DataLen * sizeof( NvU8  ) );
  291.         if( err_ != NvSuccess )
  292.         {
  293.             err_ = NvError_BadParameter;
  294.         }
  295.     }
  296. clean:
  297.     NvOsFree( Data );
  298.     NvOsFree( Transaction );
  299.     return err_;
  300. }
  301.  
  302. static NvError NvRmI2cClose_dispatch_( void *InBuffer, NvU32 InSize, void *OutBuffer, NvU32 OutSize, NvDispatchCtx* Ctx )
  303. {
  304.     NvError err_ = NvSuccess;
  305.     NvRmI2cClose_in *p_in;
  306.  
  307.     p_in = (NvRmI2cClose_in *)InBuffer;
  308.  
  309.     NvRtFreeObjRef(Ctx, NvRtObjType_NvRm_NvRmI2cHandle, p_in->hI2c);
  310.  
  311.     NvRmI2cClose( p_in->hI2c );
  312.  
  313.     return err_;
  314. }
  315.  
  316. static NvError NvRmI2cOpen_dispatch_( void *InBuffer, NvU32 InSize, void *OutBuffer, NvU32 OutSize, NvDispatchCtx* Ctx )
  317. {
  318.     NvError err_ = NvSuccess;
  319.     NvRmI2cOpen_in *p_in;
  320.     NvRmI2cOpen_out *p_out;
  321.     NvRtObjRefHandle ref_phI2c = 0;
  322.  
  323.     p_in = (NvRmI2cOpen_in *)InBuffer;
  324.     p_out = (NvRmI2cOpen_out *)((NvU8 *)OutBuffer + OFFSET(NvRmI2cOpen_params, out) - OFFSET(NvRmI2cOpen_params, inout));
  325.  
  326.     p_out->ret_ = NvRmI2cOpen( p_in->hDevice, p_in->IoModule, p_in->instance, &p_out->phI2c );
  327.     if (p_out->ret_ != NvSuccess)
  328.     {
  329.         err_ = p_out->ret_;
  330.         goto clean;
  331.     }
  332.     err_ = NvRtAllocObjRef(Ctx, &ref_phI2c);
  333.     if (err_ == NvSuccess)
  334.     {
  335.         NvRtStoreObjRef(Ctx, ref_phI2c, NvRtObjType_NvRm_NvRmI2cHandle, p_out->phI2c);
  336.     }
  337.     else
  338.     {
  339.         NvRmI2cClose(p_out->phI2c);
  340.     }
  341.  
  342. clean:
  343.     return err_;
  344. }
  345.  
  346. NvError nvrm_i2c_Dispatch( NvU32 function, void *InBuffer, NvU32 InSize, void *OutBuffer, NvU32 OutSize, NvDispatchCtx* Ctx );
  347. NvError nvrm_i2c_Dispatch( NvU32 function, void *InBuffer, NvU32 InSize, void *OutBuffer, NvU32 OutSize, NvDispatchCtx* Ctx )
  348. {
  349.     NvError err_ = NvSuccess;
  350.  
  351.     switch( function ) {
  352.     case 2:
  353.         err_ = NvRmI2cTransaction_dispatch_( InBuffer, InSize, OutBuffer, OutSize, Ctx );
  354.         break;
  355.     case 1:
  356.         err_ = NvRmI2cClose_dispatch_( InBuffer, InSize, OutBuffer, OutSize, Ctx );
  357.         break;
  358.     case 0:
  359.         err_ = NvRmI2cOpen_dispatch_( InBuffer, InSize, OutBuffer, OutSize, Ctx );
  360.         break;
  361.     default:
  362.         err_ = NvError_BadParameter;
  363.         break;
  364.     }
  365.  
  366.     return err_;
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement