bif-core.c 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842
  1. /* Copyright (c) 2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/bitrev.h>
  14. #include <linux/crc-ccitt.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/of.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/bif/consumer.h>
  26. #include <linux/bif/driver.h>
  27. /**
  28. * struct bif_ctrl_dev - holds controller device specific information
  29. * @list: Doubly-linked list parameter linking to other
  30. * BIF controllers registered in the system
  31. * @desc: Description structure for this BIF controller
  32. * @mutex: Mutex lock that is used to ensure mutual
  33. * exclusion between transactions performed on the
  34. * BIF bus for this controller
  35. * @ctrl_dev: Device pointer to the BIF controller device
  36. * @driver_data: Private data used by the BIF controller
  37. * @selected_sdev: Slave device that is currently selected on
  38. * the BIF bus of this controller
  39. * @bus_change_notifier: Head of a notifier list containing notifier
  40. * blocks that are notified when the battery
  41. * presence changes
  42. * @enter_irq_mode_work: Work task that is scheduled after a transaction
  43. * completes when there are consumers that are
  44. * actively monitoring BIF slave interrupts
  45. * @irq_count: This is a count of the total number of BIF slave
  46. * interrupts that are currently being monitored
  47. * for the BIF slaves connected to this BIF
  48. * controller
  49. * @irq_mode_delay_jiffies: Number of jiffies to wait before scheduling the
  50. * enter IRQ mode task. Using a larger value
  51. * helps to improve the performance of BIF
  52. * consumers that perform many BIF transactions.
  53. * Using a smaller value reduces the latency of
  54. * BIF slave interrupts.
  55. * @battery_present: Cached value of the battery presence. This is
  56. * used to filter out spurious presence update
  57. * calls when the battery presence state has not
  58. * changed.
  59. */
  60. struct bif_ctrl_dev {
  61. struct list_head list;
  62. struct bif_ctrl_desc *desc;
  63. struct mutex mutex;
  64. struct device *ctrl_dev;
  65. void *driver_data;
  66. struct bif_slave_dev *selected_sdev;
  67. struct blocking_notifier_head bus_change_notifier;
  68. struct delayed_work enter_irq_mode_work;
  69. int irq_count;
  70. int irq_mode_delay_jiffies;
  71. bool battery_present;
  72. };
  73. /**
  74. * struct bif_ctrl - handle used by BIF consumers for bus oriented BIF
  75. * operations
  76. * @bdev: Pointer to BIF controller device
  77. * @exclusive_lock: Flag which indicates that the BIF consumer responsible
  78. * for this handle has locked the BIF bus of this
  79. * controller. BIF transactions from other consumers are
  80. * blocked until the bus is unlocked.
  81. */
  82. struct bif_ctrl {
  83. struct bif_ctrl_dev *bdev;
  84. bool exclusive_lock;
  85. };
  86. /**
  87. * struct bif_slave_dev - holds BIF slave device information
  88. * @list: Doubly-linked list parameter linking to other
  89. * BIF slaves that have been enumerated
  90. * @bdev: Pointer to the BIF controller device that this
  91. * slave is physically connected to
  92. * @slave_addr: 8-bit BIF DEV_ADR assigned to this slave
  93. * @unique_id: 80-bit BIF unique ID of the slave
  94. * @unique_id_bits_known: Number of bits of the UID that are currently
  95. * known. This number starts is incremented during
  96. * a UID search and must end at 80 if the slave
  97. * responds to the search properly.
  98. * @present: Boolean value showing if this slave is
  99. * physically present in the system at a given
  100. * point in time. The value is set to false if the
  101. * battery pack containing the slave is
  102. * disconnected.
  103. * @l1_data: BIF DDB L1 data of the slave as read from the
  104. * slave's memory
  105. * @function_directory: Pointer to the BIF DDB L2 function directory
  106. * list as read from the slave's memory
  107. * @protocol_function: Pointer to constant protocol function data as
  108. * well as software state information if the slave
  109. * has a protocol function
  110. * @slave_ctrl_function: Pointer to constant slave control function data
  111. * as well as software state information if the
  112. * slave has a slave control function
  113. * @nvm_function: Pointer to constant non-volatile memory function
  114. * data as well as software state information if
  115. * the slave has a non-volatile memory function
  116. *
  117. * bif_slave_dev objects are stored indefinitely after enumeration in order to
  118. * speed up battery reinsertion. Only a UID check is needed after inserting a
  119. * battery assuming it has been enumerated before.
  120. *
  121. * unique_id bytes are stored such that unique_id[0] = MSB and
  122. * unique_id[BIF_UNIQUE_ID_BYTE_LENGTH - 1] = LSB
  123. */
  124. struct bif_slave_dev {
  125. struct list_head list;
  126. struct bif_ctrl_dev *bdev;
  127. u8 slave_addr;
  128. u8 unique_id[BIF_UNIQUE_ID_BYTE_LENGTH];
  129. int unique_id_bits_known;
  130. bool present;
  131. struct bif_ddb_l1_data l1_data;
  132. struct bif_ddb_l2_data *function_directory;
  133. struct bif_protocol_function *protocol_function;
  134. struct bif_slave_control_function *slave_ctrl_function;
  135. struct bif_nvm_function *nvm_function;
  136. };
  137. /**
  138. * struct bif_slave - handle used by BIF consumers for slave oriented BIF
  139. * operations
  140. * @ctrl: Consumer BIF controller handle data
  141. * @sdev: Pointer to BIF slave device
  142. */
  143. struct bif_slave {
  144. struct bif_ctrl ctrl;
  145. struct bif_slave_dev *sdev;
  146. };
  147. /* Number of times to retry a full BIF transaction before returning an error. */
  148. #define BIF_TRANSACTION_RETRY_COUNT 5
  149. static DEFINE_MUTEX(bif_ctrl_list_mutex);
  150. static LIST_HEAD(bif_ctrl_list);
  151. static DEFINE_MUTEX(bif_sdev_list_mutex);
  152. static LIST_HEAD(bif_sdev_list);
  153. static u8 next_dev_addr = 0x02;
  154. #define DEBUG_PRINT_BUFFER_SIZE 256
  155. static void fill_string(char *str, size_t str_len, u8 *buf, int buf_len)
  156. {
  157. int pos = 0;
  158. int i;
  159. for (i = 0; i < buf_len; i++) {
  160. pos += scnprintf(str + pos, str_len - pos, "0x%02X", buf[i]);
  161. if (i < buf_len - 1)
  162. pos += scnprintf(str + pos, str_len - pos, ", ");
  163. }
  164. }
  165. static void bif_print_slave_data(struct bif_slave_dev *sdev)
  166. {
  167. char str[DEBUG_PRINT_BUFFER_SIZE];
  168. u8 *uid;
  169. int i, j;
  170. struct bif_object *object;
  171. if (sdev->unique_id_bits_known != BIF_UNIQUE_ID_BIT_LENGTH)
  172. return;
  173. uid = sdev->unique_id;
  174. pr_debug("BIF slave: 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  175. uid[0], uid[1], uid[2], uid[3], uid[4], uid[5], uid[6],
  176. uid[7], uid[8], uid[9]);
  177. pr_debug(" present=%d, dev_adr=0x%02X\n", sdev->present,
  178. sdev->slave_addr);
  179. pr_debug(" revision=0x%02X, level=0x%02X, device class=0x%04X\n",
  180. sdev->l1_data.revision, sdev->l1_data.level,
  181. sdev->l1_data.device_class);
  182. pr_debug(" manufacturer ID=0x%04X, product ID=0x%04X\n",
  183. sdev->l1_data.manufacturer_id, sdev->l1_data.product_id);
  184. pr_debug(" function directory length=%d\n", sdev->l1_data.length);
  185. for (i = 0; i < sdev->l1_data.length / 4; i++) {
  186. pr_debug(" Function %d: type=0x%02X, version=0x%02X, pointer=0x%04X\n",
  187. i, sdev->function_directory[i].function_type,
  188. sdev->function_directory[i].function_version,
  189. sdev->function_directory[i].function_pointer);
  190. }
  191. if (sdev->nvm_function) {
  192. pr_debug(" NVM function: pointer=0x%04X, task=%d, wr_buf_size=%d, nvm_base=0x%04X, nvm_size=%d, nvm_lock_offset=%d\n",
  193. sdev->nvm_function->nvm_pointer,
  194. sdev->nvm_function->slave_control_channel,
  195. (sdev->nvm_function->write_buffer_size
  196. ? sdev->nvm_function->write_buffer_size : 0),
  197. sdev->nvm_function->nvm_base_address,
  198. sdev->nvm_function->nvm_size,
  199. sdev->nvm_function->nvm_lock_offset);
  200. if (sdev->nvm_function->object_count)
  201. pr_debug(" NVM objects:\n");
  202. i = 0;
  203. list_for_each_entry(object, &sdev->nvm_function->object_list,
  204. list) {
  205. pr_debug(" Object %d - addr=0x%04X, data len=%d, type=0x%02X, version=0x%02X, manufacturer ID=0x%04X, crc=0x%04X\n",
  206. i, object->addr, object->length - 8,
  207. object->type, object->version,
  208. object->manufacturer_id, object->crc);
  209. for (j = 0; j < DIV_ROUND_UP(object->length - 8, 16);
  210. j++) {
  211. fill_string(str, DEBUG_PRINT_BUFFER_SIZE,
  212. object->data + j * 16,
  213. min(16, object->length - 8 - (j * 16)));
  214. pr_debug(" data(0x%04X): %s\n", j * 16,
  215. str);
  216. }
  217. i++;
  218. }
  219. }
  220. }
  221. static void bif_print_slaves(void)
  222. {
  223. struct bif_slave_dev *sdev;
  224. mutex_lock(&bif_sdev_list_mutex);
  225. list_for_each_entry(sdev, &bif_sdev_list, list) {
  226. /* Skip slaves without fully known UIDs. */
  227. if (sdev->unique_id_bits_known != BIF_UNIQUE_ID_BIT_LENGTH)
  228. continue;
  229. bif_print_slave_data(sdev);
  230. }
  231. mutex_unlock(&bif_sdev_list_mutex);
  232. }
  233. static struct bif_slave_dev *bif_add_slave(struct bif_ctrl_dev *bdev)
  234. {
  235. struct bif_slave_dev *sdev;
  236. sdev = kzalloc(sizeof(struct bif_slave_dev), GFP_KERNEL);
  237. if (sdev == NULL) {
  238. pr_err("Memory allocation failed for bif_slave_dev\n");
  239. return ERR_PTR(-ENOMEM);
  240. }
  241. sdev->bdev = bdev;
  242. INIT_LIST_HEAD(&sdev->list);
  243. list_add_tail(&sdev->list, &bif_sdev_list);
  244. return sdev;
  245. }
  246. static void bif_remove_slave(struct bif_slave_dev *sdev)
  247. {
  248. list_del(&sdev->list);
  249. if (sdev->bdev->selected_sdev == sdev)
  250. sdev->bdev->selected_sdev = NULL;
  251. if (sdev->slave_ctrl_function)
  252. kfree(sdev->slave_ctrl_function->irq_notifier_list);
  253. kfree(sdev->slave_ctrl_function);
  254. kfree(sdev->protocol_function);
  255. kfree(sdev->function_directory);
  256. kfree(sdev);
  257. }
  258. /* This function assumes that the uid array is all 0 to start with. */
  259. static void set_uid_bit(u8 uid[BIF_UNIQUE_ID_BYTE_LENGTH], unsigned int bit,
  260. unsigned int value)
  261. {
  262. u8 mask;
  263. if (bit >= BIF_UNIQUE_ID_BIT_LENGTH)
  264. return;
  265. mask = 1 << (7 - (bit % 8));
  266. uid[bit / 8] &= ~mask;
  267. uid[bit / 8] |= value << (7 - (bit % 8));
  268. }
  269. static unsigned int get_uid_bit(u8 uid[BIF_UNIQUE_ID_BYTE_LENGTH],
  270. unsigned int bit)
  271. {
  272. if (bit >= BIF_UNIQUE_ID_BIT_LENGTH)
  273. return 0;
  274. return (uid[bit / 8] & (1 << (7 - (bit % 8)))) ? 1 : 0;
  275. }
  276. static void bif_enter_irq_mode_work(struct work_struct *work)
  277. {
  278. struct delayed_work *dwork = to_delayed_work(work);
  279. struct bif_ctrl_dev *bdev
  280. = container_of(dwork, struct bif_ctrl_dev, enter_irq_mode_work);
  281. int rc, i;
  282. mutex_lock(&bdev->mutex);
  283. for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
  284. rc = bdev->desc->ops->set_bus_state(bdev,
  285. BIF_BUS_STATE_INTERRUPT);
  286. if (rc == 0)
  287. break;
  288. }
  289. mutex_unlock(&bdev->mutex);
  290. /* Reschedule the task if the transaction failed. */
  291. if (rc) {
  292. pr_err("Could not set BIF bus to interrupt mode, rc=%d\n", rc);
  293. schedule_delayed_work(&bdev->enter_irq_mode_work,
  294. bdev->irq_mode_delay_jiffies);
  295. }
  296. }
  297. static void bif_cancel_irq_mode_work(struct bif_ctrl_dev *bdev)
  298. {
  299. cancel_delayed_work(&bdev->enter_irq_mode_work);
  300. }
  301. static void bif_schedule_irq_mode_work(struct bif_ctrl_dev *bdev)
  302. {
  303. if (bdev->irq_count > 0 &&
  304. bdev->desc->ops->get_bus_state(bdev) != BIF_BUS_STATE_INTERRUPT)
  305. schedule_delayed_work(&bdev->enter_irq_mode_work,
  306. bdev->irq_mode_delay_jiffies);
  307. }
  308. static int _bif_select_slave_no_retry(struct bif_slave_dev *sdev)
  309. {
  310. struct bif_ctrl_dev *bdev = sdev->bdev;
  311. int rc = 0;
  312. int i;
  313. /* Check if the slave is already selected. */
  314. if (sdev->bdev->selected_sdev == sdev)
  315. return 0;
  316. if (sdev->slave_addr) {
  317. /* Select using DEV_ADR. */
  318. rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_SDA,
  319. sdev->slave_addr);
  320. if (!rc)
  321. sdev->bdev->selected_sdev = sdev;
  322. } else if (sdev->unique_id_bits_known == BIF_UNIQUE_ID_BIT_LENGTH) {
  323. /* Select using full UID. */
  324. for (i = 0; i < BIF_UNIQUE_ID_BYTE_LENGTH - 1; i++) {
  325. rc = bdev->desc->ops->bus_transaction(bdev,
  326. BIF_TRANS_EDA, sdev->unique_id[i]);
  327. if (rc)
  328. goto out;
  329. }
  330. rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_SDA,
  331. sdev->unique_id[BIF_UNIQUE_ID_BYTE_LENGTH - 1]);
  332. if (rc)
  333. goto out;
  334. } else {
  335. pr_err("Cannot select slave because it has neither UID nor DEV_ADR.\n");
  336. return -EINVAL;
  337. }
  338. sdev->bdev->selected_sdev = sdev;
  339. return 0;
  340. out:
  341. pr_err("bus_transaction failed, rc=%d\n", rc);
  342. return rc;
  343. }
  344. static int bif_select_slave(struct bif_slave_dev *sdev)
  345. {
  346. int rc = -EPERM;
  347. int i;
  348. for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
  349. rc = _bif_select_slave_no_retry(sdev);
  350. if (rc == 0)
  351. break;
  352. /* Force slave reselection. */
  353. sdev->bdev->selected_sdev = NULL;
  354. }
  355. return rc;
  356. }
  357. /*
  358. * Returns 1 if slave is selected, 0 if slave is not selected, or errno if
  359. * error.
  360. */
  361. static int bif_is_slave_selected(struct bif_ctrl_dev *bdev)
  362. {
  363. int rc = -EPERM;
  364. int tack, i;
  365. for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
  366. /* Attempt a transaction query. */
  367. rc = bdev->desc->ops->bus_transaction_read(bdev, BIF_TRANS_BC,
  368. BIF_CMD_TQ, &tack);
  369. if (rc == 0 || rc == -ETIMEDOUT)
  370. break;
  371. }
  372. if (rc == 0)
  373. rc = 1;
  374. else if (rc == -ETIMEDOUT)
  375. rc = 0;
  376. else
  377. pr_err("BIF bus_transaction_read failed, rc=%d\n", rc);
  378. return rc;
  379. }
  380. /* Read from a specified number of consecutive registers. */
  381. static int _bif_slave_read_no_retry(struct bif_slave_dev *sdev, u16 addr,
  382. u8 *buf, int len)
  383. {
  384. struct bif_ctrl_dev *bdev = sdev->bdev;
  385. int rc = 0;
  386. int i, response;
  387. rc = bif_select_slave(sdev);
  388. if (rc)
  389. return rc;
  390. if (bdev->desc->ops->read_slave_registers) {
  391. /*
  392. * Use low level slave register read implementation in order to
  393. * receive the benefits of BIF burst reads.
  394. */
  395. rc = bdev->desc->ops->read_slave_registers(bdev, addr, buf,
  396. len);
  397. if (rc)
  398. pr_debug("read_slave_registers failed, rc=%d\n", rc);
  399. else
  400. return rc;
  401. /*
  402. * Fall back on individual transactions if high level register
  403. * read failed.
  404. */
  405. }
  406. for (i = 0; i < len; i++) {
  407. rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_ERA,
  408. addr >> 8);
  409. if (rc) {
  410. pr_err("bus_transaction failed, rc=%d\n", rc);
  411. return rc;
  412. }
  413. rc = bdev->desc->ops->bus_transaction_read(bdev, BIF_TRANS_RRA,
  414. addr & 0xFF, &response);
  415. if (rc) {
  416. pr_err("bus_transaction_read failed, rc=%d\n", rc);
  417. return rc;
  418. }
  419. if (!(response & BIF_SLAVE_RD_ACK)) {
  420. pr_err("BIF register read error=0x%02X\n",
  421. response & BIF_SLAVE_RD_ERR);
  422. return -EIO;
  423. }
  424. buf[i] = response & BIF_SLAVE_RD_DATA;
  425. addr++;
  426. }
  427. return rc;
  428. }
  429. /*
  430. * Read from a specified number of consecutive registers. Retry the transaction
  431. * several times in case of communcation failures.
  432. */
  433. static int _bif_slave_read(struct bif_slave_dev *sdev, u16 addr, u8 *buf,
  434. int len)
  435. {
  436. int rc = -EPERM;
  437. int i;
  438. for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
  439. rc = _bif_slave_read_no_retry(sdev, addr, buf, len);
  440. if (rc == 0)
  441. break;
  442. /* Force slave reselection. */
  443. sdev->bdev->selected_sdev = NULL;
  444. }
  445. return rc;
  446. }
  447. /* Write to a specified number of consecutive registers. */
  448. static int _bif_slave_write_no_retry(struct bif_slave_dev *sdev, u16 addr,
  449. u8 *buf, int len)
  450. {
  451. struct bif_ctrl_dev *bdev = sdev->bdev;
  452. int rc = 0;
  453. int i;
  454. rc = bif_select_slave(sdev);
  455. if (rc)
  456. return rc;
  457. if (bdev->desc->ops->write_slave_registers) {
  458. /*
  459. * Use low level slave register write implementation in order to
  460. * receive the benefits of BIF burst writes.
  461. */
  462. rc = bdev->desc->ops->write_slave_registers(bdev, addr, buf,
  463. len);
  464. if (rc)
  465. pr_debug("write_slave_registers failed, rc=%d\n", rc);
  466. else
  467. return rc;
  468. /*
  469. * Fall back on individual transactions if high level register
  470. * write failed.
  471. */
  472. }
  473. rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_ERA, addr >> 8);
  474. if (rc)
  475. goto out;
  476. rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_WRA, addr & 0xFF);
  477. if (rc)
  478. goto out;
  479. for (i = 0; i < len; i++) {
  480. rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_WD,
  481. buf[i]);
  482. if (rc)
  483. goto out;
  484. }
  485. return 0;
  486. out:
  487. pr_err("bus_transaction failed, rc=%d\n", rc);
  488. return rc;
  489. }
  490. /*
  491. * Write to a specified number of consecutive registers. Retry the transaction
  492. * several times in case of communcation failures.
  493. */
  494. static int _bif_slave_write(struct bif_slave_dev *sdev, u16 addr, u8 *buf,
  495. int len)
  496. {
  497. int rc = -EPERM;
  498. int i;
  499. for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
  500. rc = _bif_slave_write_no_retry(sdev, addr, buf, len);
  501. if (rc == 0)
  502. break;
  503. /* Force slave reselection. */
  504. sdev->bdev->selected_sdev = NULL;
  505. }
  506. return rc;
  507. }
  508. /* Perform a read-modify-write sequence on a single BIF slave register. */
  509. static int _bif_slave_masked_write(struct bif_slave_dev *sdev, u16 addr, u8 val,
  510. u8 mask)
  511. {
  512. int rc;
  513. u8 reg;
  514. rc = _bif_slave_read(sdev, addr, &reg, 1);
  515. if (rc)
  516. return rc;
  517. reg = (reg & ~mask) | (val & mask);
  518. return _bif_slave_write(sdev, addr, &reg, 1);
  519. }
  520. static int _bif_check_task(struct bif_slave_dev *sdev, unsigned int task)
  521. {
  522. if (IS_ERR_OR_NULL(sdev)) {
  523. pr_err("Invalid slave device handle=%ld\n", PTR_ERR(sdev));
  524. return -EINVAL;
  525. } else if (!sdev->bdev) {
  526. pr_err("BIF controller has been removed\n");
  527. return -ENXIO;
  528. } else if (!sdev->slave_ctrl_function
  529. || sdev->slave_ctrl_function->task_count == 0) {
  530. pr_err("BIF slave does not support slave control\n");
  531. return -ENODEV;
  532. } else if (task >= sdev->slave_ctrl_function->task_count) {
  533. pr_err("Requested task: %u greater than max: %u for this slave\n",
  534. task, sdev->slave_ctrl_function->task_count);
  535. return -EINVAL;
  536. }
  537. return 0;
  538. }
  539. static int _bif_task_is_busy(struct bif_slave_dev *sdev, unsigned int task)
  540. {
  541. int rc;
  542. u16 addr;
  543. u8 reg = 0;
  544. rc = _bif_check_task(sdev, task);
  545. if (rc) {
  546. pr_err("Invalid slave device or task, rc=%d\n", rc);
  547. return rc;
  548. }
  549. /* Check the task busy state. */
  550. addr = SLAVE_CTRL_FUNC_TASK_BUSY_ADDR(
  551. sdev->slave_ctrl_function->slave_ctrl_pointer, task);
  552. rc = _bif_slave_read(sdev, addr, &reg, 1);
  553. if (rc) {
  554. pr_err("BIF slave register read failed, rc=%d\n", rc);
  555. return rc;
  556. }
  557. return (reg & BIT(task % SLAVE_CTRL_TASKS_PER_SET)) ? 1 : 0;
  558. }
  559. static int _bif_enable_auto_task(struct bif_slave_dev *sdev, unsigned int task)
  560. {
  561. int rc;
  562. u16 addr;
  563. u8 mask;
  564. rc = _bif_check_task(sdev, task);
  565. if (rc) {
  566. pr_err("Invalid slave device or task, rc=%d\n", rc);
  567. return rc;
  568. }
  569. /* Enable the auto task within the slave */
  570. mask = BIT(task % SLAVE_CTRL_TASKS_PER_SET);
  571. addr = SLAVE_CTRL_FUNC_TASK_AUTO_TRIGGER_ADDR(
  572. sdev->slave_ctrl_function->slave_ctrl_pointer, task);
  573. if (task / SLAVE_CTRL_TASKS_PER_SET == 0) {
  574. /* Set global auto task enable. */
  575. mask |= BIT(0);
  576. }
  577. rc = _bif_slave_masked_write(sdev, addr, 0xFF, mask);
  578. if (rc) {
  579. pr_err("BIF slave register masked write failed, rc=%d\n", rc);
  580. return rc;
  581. }
  582. /* Set global auto task enable if task not in set 0. */
  583. if (task / SLAVE_CTRL_TASKS_PER_SET != 0) {
  584. addr = SLAVE_CTRL_FUNC_TASK_AUTO_TRIGGER_ADDR(
  585. sdev->slave_ctrl_function->slave_ctrl_pointer, 0);
  586. rc = _bif_slave_masked_write(sdev, addr, 0xFF, BIT(0));
  587. if (rc) {
  588. pr_err("BIF slave register masked write failed, rc=%d\n",
  589. rc);
  590. return rc;
  591. }
  592. }
  593. return rc;
  594. }
  595. static int _bif_disable_auto_task(struct bif_slave_dev *sdev, unsigned int task)
  596. {
  597. int rc;
  598. u16 addr;
  599. u8 mask;
  600. rc = _bif_check_task(sdev, task);
  601. if (rc) {
  602. pr_err("Invalid slave or task, rc=%d\n", rc);
  603. return rc;
  604. }
  605. /* Disable the auto task within the slave */
  606. mask = BIT(task % SLAVE_CTRL_TASKS_PER_SET);
  607. addr = SLAVE_CTRL_FUNC_TASK_AUTO_TRIGGER_ADDR(
  608. sdev->slave_ctrl_function->slave_ctrl_pointer, task);
  609. rc = _bif_slave_masked_write(sdev, addr, 0x00, mask);
  610. if (rc) {
  611. pr_err("BIF slave register masked write failed, rc=%d\n", rc);
  612. return rc;
  613. }
  614. return rc;
  615. }
  616. /*
  617. * The MIPI-BIF spec does not define a maximum time in which an NVM write must
  618. * complete. The following delay and recheck count therefore represent
  619. * arbitrary but reasonable values.
  620. */
  621. #define NVM_WRITE_POLL_DELAY_MS 20
  622. #define NVM_WRITE_MAX_POLL_COUNT 50
  623. static int _bif_slave_nvm_raw_write(struct bif_slave_dev *sdev, u16 offset,
  624. u8 *buf, int len)
  625. {
  626. int rc = 0;
  627. int write_len, poll_count, rc2;
  628. u8 write_buf[3];
  629. if (!sdev->nvm_function) {
  630. pr_err("BIF slave has no NVM function\n");
  631. return -ENODEV;
  632. } else if (offset + len > sdev->nvm_function->nvm_size) {
  633. pr_err("write offset + len = %d > NVM size = %d\n",
  634. offset + len, sdev->nvm_function->nvm_size);
  635. return -EINVAL;
  636. } else if (offset < sdev->nvm_function->nvm_lock_offset) {
  637. pr_err("write offset = %d < first writable offset = %d\n",
  638. offset, sdev->nvm_function->nvm_lock_offset);
  639. return -EINVAL;
  640. }
  641. rc = _bif_enable_auto_task(sdev,
  642. sdev->nvm_function->slave_control_channel);
  643. if (rc) {
  644. pr_err("Failed to enable NVM auto task, rc=%d\n", rc);
  645. return rc;
  646. }
  647. while (len > 0) {
  648. write_len = sdev->nvm_function->write_buffer_size;
  649. if (write_len == 0)
  650. write_len = 256;
  651. write_len = min(write_len, len);
  652. write_buf[0] = offset >> 8;
  653. write_buf[1] = offset;
  654. write_buf[2] = (write_len == 256) ? 0 : write_len;
  655. /* Write offset and size registers. */
  656. rc = _bif_slave_write(sdev, sdev->nvm_function->nvm_pointer + 6,
  657. write_buf, 3);
  658. if (rc) {
  659. pr_err("BIF slave write failed, rc=%d\n", rc);
  660. goto done;
  661. }
  662. /* Write to NVM write buffer registers. */
  663. rc = _bif_slave_write(sdev, sdev->nvm_function->nvm_pointer + 9,
  664. buf, write_len);
  665. if (rc) {
  666. pr_err("BIF slave write failed, rc=%d\n", rc);
  667. goto done;
  668. }
  669. /*
  670. * Wait for completion of the NVM write which was auto-triggered
  671. * by the register write of the last byte in the NVM write
  672. * buffer.
  673. */
  674. poll_count = NVM_WRITE_MAX_POLL_COUNT;
  675. do {
  676. msleep(NVM_WRITE_POLL_DELAY_MS);
  677. rc = _bif_task_is_busy(sdev,
  678. sdev->nvm_function->slave_control_channel);
  679. poll_count--;
  680. } while (rc > 0 && poll_count > 0);
  681. if (rc < 0) {
  682. pr_err("Failed to check task state, rc=%d", rc);
  683. goto done;
  684. } else if (rc > 0) {
  685. pr_err("BIF slave NVM write not completed after %d ms\n",
  686. NVM_WRITE_POLL_DELAY_MS * NVM_WRITE_MAX_POLL_COUNT);
  687. rc = -ETIMEDOUT;
  688. goto done;
  689. }
  690. len -= write_len;
  691. offset += write_len;
  692. buf += write_len;
  693. }
  694. done:
  695. rc2 = _bif_disable_auto_task(sdev,
  696. sdev->nvm_function->slave_control_channel);
  697. if (rc2) {
  698. pr_err("Failed to disable NVM auto task, rc=%d\n", rc2);
  699. return rc2;
  700. }
  701. return rc;
  702. }
  703. /* Takes a mutex if this consumer is not an exclusive bus user. */
  704. static void bif_ctrl_lock(struct bif_ctrl *ctrl)
  705. {
  706. if (!ctrl->exclusive_lock) {
  707. mutex_lock(&ctrl->bdev->mutex);
  708. bif_cancel_irq_mode_work(ctrl->bdev);
  709. }
  710. }
  711. /* Releases a mutex if this consumer is not an exclusive bus user. */
  712. static void bif_ctrl_unlock(struct bif_ctrl *ctrl)
  713. {
  714. if (!ctrl->exclusive_lock) {
  715. bif_schedule_irq_mode_work(ctrl->bdev);
  716. mutex_unlock(&ctrl->bdev->mutex);
  717. }
  718. }
  719. static void bif_slave_ctrl_lock(struct bif_slave *slave)
  720. {
  721. bif_ctrl_lock(&slave->ctrl);
  722. }
  723. static void bif_slave_ctrl_unlock(struct bif_slave *slave)
  724. {
  725. bif_ctrl_unlock(&slave->ctrl);
  726. }
  727. /**
  728. * bif_crc_ccitt() - calculate the CRC-CCITT CRC value of the data specified
  729. * @buffer: Data to calculate the CRC of
  730. * @len: Length of the data buffer in bytes
  731. *
  732. * MIPI-BIF specifies the usage of CRC-CCITT for BIF data objects. This
  733. * function performs the CRC calculation while taking into account the bit
  734. * ordering used by BIF.
  735. */
  736. u16 bif_crc_ccitt(const u8 *buffer, unsigned int len)
  737. {
  738. u16 crc = 0xFFFF;
  739. while (len--) {
  740. crc = crc_ccitt_byte(crc, bitrev8(*buffer));
  741. buffer++;
  742. }
  743. return bitrev16(crc);
  744. }
  745. EXPORT_SYMBOL(bif_crc_ccitt);
  746. static u16 bif_object_crc_ccitt(const struct bif_object *object)
  747. {
  748. u16 crc = 0xFFFF;
  749. int i;
  750. crc = crc_ccitt_byte(crc, bitrev8(object->type));
  751. crc = crc_ccitt_byte(crc, bitrev8(object->version));
  752. crc = crc_ccitt_byte(crc, bitrev8(object->manufacturer_id >> 8));
  753. crc = crc_ccitt_byte(crc, bitrev8(object->manufacturer_id));
  754. crc = crc_ccitt_byte(crc, bitrev8(object->length >> 8));
  755. crc = crc_ccitt_byte(crc, bitrev8(object->length));
  756. for (i = 0; i < object->length - 8; i++)
  757. crc = crc_ccitt_byte(crc, bitrev8(object->data[i]));
  758. return bitrev16(crc);
  759. }
  760. static int bif_check_task(struct bif_slave *slave, unsigned int task)
  761. {
  762. if (IS_ERR_OR_NULL(slave)) {
  763. pr_err("Invalid slave pointer=%ld\n", PTR_ERR(slave));
  764. return -EINVAL;
  765. }
  766. return _bif_check_task(slave->sdev, task);
  767. }
  768. /**
  769. * bif_request_irq() - request a BIF slave IRQ by slave task number
  770. * @slave: BIF slave handle
  771. * @task: BIF task number of the IRQ inside of the slave. This
  772. * corresponds to the slave control channel specified for a given
  773. * BIF function inside of the slave.
  774. * @nb: Notifier block to call when the IRQ fires
  775. *
  776. * This function registers a notifier block to call when the BIF slave interrupt
  777. * is triggered and also enables the interrupt. The interrupt is enabled inside
  778. * of the BIF slave's slave control function and also the BIF bus is put into
  779. * interrupt mode.
  780. *
  781. * Returns 0 for success or errno if an error occurred.
  782. */
  783. int bif_request_irq(struct bif_slave *slave, unsigned int task,
  784. struct notifier_block *nb)
  785. {
  786. int rc;
  787. u16 addr;
  788. u8 reg, mask;
  789. rc = bif_check_task(slave, task);
  790. if (rc) {
  791. pr_err("Invalid slave or task, rc=%d\n", rc);
  792. return rc;
  793. }
  794. bif_slave_ctrl_lock(slave);
  795. rc = blocking_notifier_chain_register(
  796. &slave->sdev->slave_ctrl_function->irq_notifier_list[task], nb);
  797. if (rc) {
  798. pr_err("Notifier registration failed, rc=%d\n", rc);
  799. goto done;
  800. }
  801. /* Enable the interrupt within the slave */
  802. mask = BIT(task % SLAVE_CTRL_TASKS_PER_SET);
  803. addr = SLAVE_CTRL_FUNC_IRQ_EN_ADDR(
  804. slave->sdev->slave_ctrl_function->slave_ctrl_pointer, task);
  805. if (task / SLAVE_CTRL_TASKS_PER_SET == 0) {
  806. /* Set global interrupt enable. */
  807. mask |= BIT(0);
  808. }
  809. rc = _bif_slave_read(slave->sdev, addr, &reg, 1);
  810. if (rc) {
  811. pr_err("BIF slave register read failed, rc=%d\n", rc);
  812. goto notifier_unregister;
  813. }
  814. reg |= mask;
  815. rc = _bif_slave_write(slave->sdev, addr, &reg, 1);
  816. if (rc) {
  817. pr_err("BIF slave register write failed, rc=%d\n", rc);
  818. goto notifier_unregister;
  819. }
  820. /* Set global interrupt enable if task not in set 0. */
  821. if (task / SLAVE_CTRL_TASKS_PER_SET != 0) {
  822. mask = BIT(0);
  823. addr = SLAVE_CTRL_FUNC_IRQ_EN_ADDR(
  824. slave->sdev->slave_ctrl_function->slave_ctrl_pointer, 0);
  825. rc = _bif_slave_read(slave->sdev, addr, &reg, 1);
  826. if (rc) {
  827. pr_err("BIF slave register read failed, rc=%d\n", rc);
  828. goto notifier_unregister;
  829. }
  830. reg |= mask;
  831. rc = _bif_slave_write(slave->sdev, addr, &reg, 1);
  832. if (rc) {
  833. pr_err("BIF slave register write failed, rc=%d\n", rc);
  834. goto notifier_unregister;
  835. }
  836. }
  837. rc = slave->sdev->bdev->desc->ops->set_bus_state(slave->sdev->bdev,
  838. BIF_BUS_STATE_INTERRUPT);
  839. if (rc) {
  840. pr_err("Could not set BIF bus to interrupt mode, rc=%d\n", rc);
  841. goto notifier_unregister;
  842. }
  843. slave->sdev->bdev->irq_count++;
  844. done:
  845. bif_slave_ctrl_unlock(slave);
  846. return rc;
  847. notifier_unregister:
  848. blocking_notifier_chain_unregister(
  849. &slave->sdev->slave_ctrl_function->irq_notifier_list[task],
  850. nb);
  851. bif_slave_ctrl_unlock(slave);
  852. return rc;
  853. }
  854. EXPORT_SYMBOL(bif_request_irq);
  855. /**
  856. * bif_free_irq() - free a BIF slave IRQ by slave task number
  857. * @slave: BIF slave handle
  858. * @task: BIF task number of the IRQ inside of the slave. This
  859. * corresponds to the slave control channel specified for a given
  860. * BIF function inside of the slave.
  861. * @nb: Notifier block previously registered with this interrupt
  862. *
  863. * This function unregisters a notifier block that was previously registered
  864. * with bif_request_irq().
  865. *
  866. * Returns 0 for success or errno if an error occurred.
  867. */
  868. int bif_free_irq(struct bif_slave *slave, unsigned int task,
  869. struct notifier_block *nb)
  870. {
  871. int rc;
  872. u16 addr;
  873. u8 reg;
  874. rc = bif_check_task(slave, task);
  875. if (rc) {
  876. pr_err("Invalid slave or task, rc=%d\n", rc);
  877. return rc;
  878. }
  879. bif_slave_ctrl_lock(slave);
  880. /* Disable the interrupt within the slave */
  881. reg = BIT(task % SLAVE_CTRL_TASKS_PER_SET);
  882. addr = SLAVE_CTRL_FUNC_IRQ_CLEAR_ADDR(
  883. slave->sdev->slave_ctrl_function->slave_ctrl_pointer, task);
  884. rc = _bif_slave_write(slave->sdev, addr, &reg, 1);
  885. if (rc) {
  886. pr_err("BIF slave register write failed, rc=%d\n", rc);
  887. goto done;
  888. }
  889. rc = blocking_notifier_chain_unregister(
  890. &slave->sdev->slave_ctrl_function->irq_notifier_list[task], nb);
  891. if (rc) {
  892. pr_err("Notifier unregistration failed, rc=%d\n", rc);
  893. goto done;
  894. }
  895. slave->sdev->bdev->irq_count--;
  896. if (slave->sdev->bdev->irq_count == 0) {
  897. bif_cancel_irq_mode_work(slave->sdev->bdev);
  898. } else if (slave->sdev->bdev->irq_count < 0) {
  899. pr_err("Unbalanced IRQ free.\n");
  900. rc = -EINVAL;
  901. slave->sdev->bdev->irq_count = 0;
  902. }
  903. done:
  904. bif_slave_ctrl_unlock(slave);
  905. return rc;
  906. }
  907. EXPORT_SYMBOL(bif_free_irq);
  908. /**
  909. * bif_trigger_task() - trigger a task within a BIF slave
  910. * @slave: BIF slave handle
  911. * @task: BIF task inside of the slave to trigger. This corresponds to
  912. * the slave control channel specified for a given BIF function
  913. * inside of the slave.
  914. *
  915. * Returns 0 for success or errno if an error occurred.
  916. */
  917. int bif_trigger_task(struct bif_slave *slave, unsigned int task)
  918. {
  919. int rc;
  920. u16 addr;
  921. u8 reg;
  922. rc = bif_check_task(slave, task);
  923. if (rc) {
  924. pr_err("Invalid slave or task, rc=%d\n", rc);
  925. return rc;
  926. }
  927. bif_slave_ctrl_lock(slave);
  928. /* Trigger the task within the slave. */
  929. reg = BIT(task % SLAVE_CTRL_TASKS_PER_SET);
  930. addr = SLAVE_CTRL_FUNC_TASK_TRIGGER_ADDR(
  931. slave->sdev->slave_ctrl_function->slave_ctrl_pointer, task);
  932. rc = _bif_slave_write(slave->sdev, addr, &reg, 1);
  933. if (rc) {
  934. pr_err("BIF slave register write failed, rc=%d\n", rc);
  935. goto done;
  936. }
  937. done:
  938. bif_slave_ctrl_unlock(slave);
  939. return rc;
  940. }
  941. EXPORT_SYMBOL(bif_trigger_task);
  942. /**
  943. * bif_enable_auto_task() - enable task auto triggering for the specified task
  944. * @slave: BIF slave handle
  945. * @task: BIF task inside of the slave to configure for automatic
  946. * triggering. This corresponds to the slave control channel
  947. * specified for a given BIF function inside of the slave.
  948. *
  949. * Returns 0 for success or errno if an error occurred.
  950. */
  951. int bif_enable_auto_task(struct bif_slave *slave, unsigned int task)
  952. {
  953. int rc;
  954. if (IS_ERR_OR_NULL(slave)) {
  955. pr_err("Invalid slave pointer=%ld\n", PTR_ERR(slave));
  956. return -EINVAL;
  957. }
  958. bif_slave_ctrl_lock(slave);
  959. rc = _bif_enable_auto_task(slave->sdev, task);
  960. bif_slave_ctrl_unlock(slave);
  961. return rc;
  962. }
  963. EXPORT_SYMBOL(bif_enable_auto_task);
  964. /**
  965. * bif_disable_auto_task() - disable task auto triggering for the specified task
  966. * @slave: BIF slave handle
  967. * @task: BIF task inside of the slave to stop automatic triggering on.
  968. * This corresponds to the slave control channel specified for a
  969. * given BIF function inside of the slave.
  970. *
  971. * This function should be called after bif_enable_auto_task() in a paired
  972. * fashion.
  973. *
  974. * Returns 0 for success or errno if an error occurred.
  975. */
  976. int bif_disable_auto_task(struct bif_slave *slave, unsigned int task)
  977. {
  978. int rc;
  979. if (IS_ERR_OR_NULL(slave)) {
  980. pr_err("Invalid slave pointer=%ld\n", PTR_ERR(slave));
  981. return -EINVAL;
  982. }
  983. bif_slave_ctrl_lock(slave);
  984. rc = _bif_disable_auto_task(slave->sdev, task);
  985. bif_slave_ctrl_unlock(slave);
  986. return rc;
  987. }
  988. EXPORT_SYMBOL(bif_disable_auto_task);
  989. /**
  990. * bif_task_is_busy() - checks the state of a BIF slave task
  991. * @slave: BIF slave handle
  992. * @task: BIF task inside of the slave to trigger. This corresponds to
  993. * the slave control channel specified for a given BIF function
  994. * inside of the slave.
  995. *
  996. * Returns 1 if the task is busy, 0 if it is not busy, and errno on error.
  997. */
  998. int bif_task_is_busy(struct bif_slave *slave, unsigned int task)
  999. {
  1000. int rc;
  1001. if (IS_ERR_OR_NULL(slave)) {
  1002. pr_err("Invalid slave pointer=%ld\n", PTR_ERR(slave));
  1003. return -EINVAL;
  1004. }
  1005. bif_slave_ctrl_lock(slave);
  1006. rc = _bif_task_is_busy(slave->sdev, task);
  1007. bif_slave_ctrl_unlock(slave);
  1008. return rc;
  1009. }
  1010. EXPORT_SYMBOL(bif_task_is_busy);
  1011. static int bif_slave_notify_irqs(struct bif_slave_dev *sdev, int set, u8 val)
  1012. {
  1013. int rc = 0;
  1014. int i, task;
  1015. for (i = 0; i < SLAVE_CTRL_TASKS_PER_SET; i++) {
  1016. if (val & (1 << i)) {
  1017. task = set * SLAVE_CTRL_TASKS_PER_SET + i;
  1018. rc = blocking_notifier_call_chain(
  1019. &sdev->slave_ctrl_function->irq_notifier_list[task],
  1020. task, sdev->bdev);
  1021. rc = notifier_to_errno(rc);
  1022. if (rc)
  1023. pr_err("Notification failed for task %d\n",
  1024. task);
  1025. }
  1026. }
  1027. return rc;
  1028. }
  1029. static int bif_slave_handle_irq(struct bif_slave_dev *sdev)
  1030. {
  1031. struct bif_ctrl_dev *bdev = sdev->bdev;
  1032. bool resp = false;
  1033. int rc = 0;
  1034. int i;
  1035. u16 addr;
  1036. u8 reg;
  1037. mutex_lock(&sdev->bdev->mutex);
  1038. bif_cancel_irq_mode_work(sdev->bdev);
  1039. rc = bif_select_slave(sdev);
  1040. if (rc) {
  1041. pr_err("Could not select slave, rc=%d\n", rc);
  1042. goto done;
  1043. }
  1044. /* Check overall slave interrupt status. */
  1045. rc = bdev->desc->ops->bus_transaction_query(bdev, BIF_TRANS_BC,
  1046. BIF_CMD_ISTS, &resp);
  1047. if (rc) {
  1048. pr_err("Could not query slave interrupt status, rc=%d\n", rc);
  1049. goto done;
  1050. }
  1051. if (resp) {
  1052. for (i = 0; i < sdev->slave_ctrl_function->task_count
  1053. / SLAVE_CTRL_TASKS_PER_SET; i++) {
  1054. addr = sdev->slave_ctrl_function->slave_ctrl_pointer
  1055. + 4 * i + 1;
  1056. rc = _bif_slave_read(sdev, addr, &reg, 1);
  1057. if (rc) {
  1058. pr_err("BIF slave register read failed, rc=%d\n",
  1059. rc);
  1060. goto done;
  1061. }
  1062. /* Ensure that interrupts are pending in the set. */
  1063. if (reg != 0x00) {
  1064. /*
  1065. * Release mutex before notifying consumers so
  1066. * that they can use the bus.
  1067. */
  1068. mutex_unlock(&sdev->bdev->mutex);
  1069. rc = bif_slave_notify_irqs(sdev, i, reg);
  1070. if (rc) {
  1071. pr_err("BIF slave irq notification failed, rc=%d\n",
  1072. rc);
  1073. goto notification_failed;
  1074. }
  1075. mutex_lock(&sdev->bdev->mutex);
  1076. rc = bif_select_slave(sdev);
  1077. if (rc) {
  1078. pr_err("Could not select slave, rc=%d\n",
  1079. rc);
  1080. goto done;
  1081. }
  1082. /* Clear all interrupts in this set. */
  1083. rc = _bif_slave_write(sdev, addr, &reg, 1);
  1084. if (rc) {
  1085. pr_err("BIF slave register write failed, rc=%d\n",
  1086. rc);
  1087. goto done;
  1088. }
  1089. }
  1090. }
  1091. }
  1092. done:
  1093. bif_schedule_irq_mode_work(sdev->bdev);
  1094. mutex_unlock(&sdev->bdev->mutex);
  1095. notification_failed:
  1096. if (rc == 0)
  1097. rc = resp;
  1098. return rc;
  1099. }
  1100. /**
  1101. * bif_ctrl_notify_slave_irq() - notify the BIF framework that a slave interrupt
  1102. * was received by a BIF controller
  1103. * @bdev: BIF controller device pointer
  1104. *
  1105. * This function should only be called from a BIF controller driver.
  1106. *
  1107. * Returns 0 for success or errno if an error occurred.
  1108. */
  1109. int bif_ctrl_notify_slave_irq(struct bif_ctrl_dev *bdev)
  1110. {
  1111. struct bif_slave_dev *sdev;
  1112. int rc = 0, handled = 0;
  1113. if (IS_ERR_OR_NULL(bdev))
  1114. return -EINVAL;
  1115. mutex_lock(&bif_sdev_list_mutex);
  1116. list_for_each_entry(sdev, &bif_sdev_list, list) {
  1117. if (sdev->bdev == bdev && sdev->present) {
  1118. rc = bif_slave_handle_irq(sdev);
  1119. if (rc < 0) {
  1120. pr_err("Could not handle BIF slave irq, rc=%d\n",
  1121. rc);
  1122. break;
  1123. }
  1124. handled += rc;
  1125. }
  1126. }
  1127. mutex_unlock(&bif_sdev_list_mutex);
  1128. if (handled == 0)
  1129. pr_info("Spurious BIF slave interrupt detected.\n");
  1130. if (rc > 0)
  1131. rc = 0;
  1132. return rc;
  1133. }
  1134. EXPORT_SYMBOL(bif_ctrl_notify_slave_irq);
  1135. /**
  1136. * bif_ctrl_notify_battery_changed() - notify the BIF framework that a battery
  1137. * pack has been inserted or removed
  1138. * @bdev: BIF controller device pointer
  1139. *
  1140. * This function should only be called from a BIF controller driver.
  1141. *
  1142. * Returns 0 for success or errno if an error occurred.
  1143. */
  1144. int bif_ctrl_notify_battery_changed(struct bif_ctrl_dev *bdev)
  1145. {
  1146. int rc = 0;
  1147. int present;
  1148. if (IS_ERR_OR_NULL(bdev))
  1149. return -EINVAL;
  1150. if (bdev->desc->ops->get_battery_presence) {
  1151. present = bdev->desc->ops->get_battery_presence(bdev);
  1152. if (present < 0) {
  1153. pr_err("Could not determine battery presence, rc=%d\n",
  1154. rc);
  1155. return rc;
  1156. }
  1157. if (bdev->battery_present == !!present)
  1158. return 0;
  1159. bdev->battery_present = present;
  1160. rc = blocking_notifier_call_chain(&bdev->bus_change_notifier,
  1161. present ? BIF_BUS_EVENT_BATTERY_INSERTED
  1162. : BIF_BUS_EVENT_BATTERY_REMOVED, bdev);
  1163. if (rc)
  1164. pr_err("Call chain noification failed, rc=%d\n", rc);
  1165. }
  1166. return rc;
  1167. }
  1168. EXPORT_SYMBOL(bif_ctrl_notify_battery_changed);
  1169. /**
  1170. * bif_ctrl_signal_battery_changed() - notify the BIF framework that a battery
  1171. * pack has been inserted or removed
  1172. * @ctrl: BIF controller consumer handle
  1173. *
  1174. * This function should only be called by a BIF consumer driver on systems where
  1175. * the BIF controller driver is unable to determine when a battery is inserted
  1176. * or removed.
  1177. *
  1178. * Returns 0 for success or errno if an error occurred.
  1179. */
  1180. int bif_ctrl_signal_battery_changed(struct bif_ctrl *ctrl)
  1181. {
  1182. if (IS_ERR_OR_NULL(ctrl))
  1183. return -EINVAL;
  1184. return bif_ctrl_notify_battery_changed(ctrl->bdev);
  1185. }
  1186. EXPORT_SYMBOL(bif_ctrl_signal_battery_changed);
  1187. /**
  1188. * bif_ctrl_notifier_register() - register a notifier block to be called when
  1189. * a battery pack is inserted or removed
  1190. * @ctrl: BIF controller consumer handle
  1191. *
  1192. * The value passed into the notifier when it is called is one of
  1193. * enum bif_bus_event.
  1194. *
  1195. * Returns 0 for success or errno if an error occurred.
  1196. */
  1197. int bif_ctrl_notifier_register(struct bif_ctrl *ctrl, struct notifier_block *nb)
  1198. {
  1199. int rc;
  1200. if (IS_ERR_OR_NULL(ctrl))
  1201. return -EINVAL;
  1202. rc = blocking_notifier_chain_register(&ctrl->bdev->bus_change_notifier,
  1203. nb);
  1204. if (rc)
  1205. pr_err("Notifier registration failed, rc=%d\n", rc);
  1206. return rc;
  1207. }
  1208. EXPORT_SYMBOL(bif_ctrl_notifier_register);
  1209. /**
  1210. * bif_ctrl_notifier_unregister() - unregister a battery status change notifier
  1211. * block that was previously registered
  1212. * @ctrl: BIF controller consumer handle
  1213. *
  1214. * Returns 0 for success or errno if an error occurred.
  1215. */
  1216. int bif_ctrl_notifier_unregister(struct bif_ctrl *ctrl,
  1217. struct notifier_block *nb)
  1218. {
  1219. int rc;
  1220. if (IS_ERR_OR_NULL(ctrl))
  1221. return -EINVAL;
  1222. rc =
  1223. blocking_notifier_chain_unregister(&ctrl->bdev->bus_change_notifier,
  1224. nb);
  1225. if (rc)
  1226. pr_err("Notifier unregistration failed, rc=%d\n", rc);
  1227. return rc;
  1228. }
  1229. EXPORT_SYMBOL(bif_ctrl_notifier_unregister);
  1230. /**
  1231. * bif_get_bus_handle() - returns the BIF controller consumer handle associated
  1232. * with a BIF slave handle
  1233. * @slave: BIF slave handle
  1234. *
  1235. * Note, bif_ctrl_put() should never be called for the pointer output by
  1236. * bif_get_bus_handle().
  1237. */
  1238. struct bif_ctrl *bif_get_bus_handle(struct bif_slave *slave)
  1239. {
  1240. if (IS_ERR_OR_NULL(slave))
  1241. return ERR_PTR(-EINVAL);
  1242. return &slave->ctrl;
  1243. }
  1244. EXPORT_SYMBOL(bif_get_bus_handle);
  1245. /**
  1246. * bif_ctrl_count() - returns the number of registered BIF controllers
  1247. */
  1248. int bif_ctrl_count(void)
  1249. {
  1250. struct bif_ctrl_dev *bdev;
  1251. int count = 0;
  1252. mutex_lock(&bif_ctrl_list_mutex);
  1253. list_for_each_entry(bdev, &bif_ctrl_list, list) {
  1254. count++;
  1255. }
  1256. mutex_unlock(&bif_ctrl_list_mutex);
  1257. return count;
  1258. }
  1259. EXPORT_SYMBOL(bif_ctrl_count);
  1260. /**
  1261. * bif_ctrl_get_by_id() - get a handle for the id'th BIF controller registered
  1262. * in the system
  1263. * @id: Arbitrary number associated with the BIF bus in the system
  1264. *
  1265. * id must be in the range [0, bif_ctrl_count() - 1]. This function should only
  1266. * need to be called by a BIF consumer that is unable to link to a given BIF
  1267. * controller via a device tree binding.
  1268. *
  1269. * Returns a BIF controller consumer handle if successful or an ERR_PTR if not.
  1270. */
  1271. struct bif_ctrl *bif_ctrl_get_by_id(unsigned int id)
  1272. {
  1273. struct bif_ctrl_dev *bdev;
  1274. struct bif_ctrl_dev *bdev_found = NULL;
  1275. struct bif_ctrl *ctrl = ERR_PTR(-ENODEV);
  1276. mutex_lock(&bif_ctrl_list_mutex);
  1277. list_for_each_entry(bdev, &bif_ctrl_list, list) {
  1278. if (id == 0) {
  1279. bdev_found = bdev;
  1280. break;
  1281. }
  1282. id--;
  1283. }
  1284. mutex_unlock(&bif_ctrl_list_mutex);
  1285. if (bdev_found) {
  1286. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  1287. if (!ctrl) {
  1288. pr_err("Bus handle allocation failed\n");
  1289. ctrl = ERR_PTR(-ENOMEM);
  1290. } else {
  1291. ctrl->bdev = bdev_found;
  1292. }
  1293. }
  1294. return ctrl;
  1295. }
  1296. EXPORT_SYMBOL(bif_ctrl_get_by_id);
  1297. /**
  1298. * bif_ctrl_get() - get a handle for the BIF controller that is linked to the
  1299. * consumer device in the device tree
  1300. * @consumer_dev: Pointer to the consumer's device
  1301. *
  1302. * In order to use this function, the BIF consumer's device must specify the
  1303. * "qcom,bif-ctrl" property in its device tree node which points to a BIF
  1304. * controller device node.
  1305. *
  1306. * Returns a BIF controller consumer handle if successful or an ERR_PTR if not.
  1307. * If the BIF controller linked to the consumer device has not yet probed, then
  1308. * ERR_PTR(-EPROBE_DEFER) is returned.
  1309. */
  1310. struct bif_ctrl *bif_ctrl_get(struct device *consumer_dev)
  1311. {
  1312. struct device_node *ctrl_node = NULL;
  1313. struct bif_ctrl_dev *bdev_found = NULL;
  1314. struct bif_ctrl *ctrl = ERR_PTR(-EPROBE_DEFER);
  1315. struct bif_ctrl_dev *bdev = NULL;
  1316. if (!consumer_dev || !consumer_dev->of_node) {
  1317. pr_err("Invalid device node\n");
  1318. return ERR_PTR(-EINVAL);
  1319. }
  1320. ctrl_node = of_parse_phandle(consumer_dev->of_node, "qcom,bif-ctrl", 0);
  1321. if (!ctrl_node) {
  1322. pr_err("Could not find qcom,bif-ctrl property in %s\n",
  1323. consumer_dev->of_node->full_name);
  1324. return ERR_PTR(-ENXIO);
  1325. }
  1326. mutex_lock(&bif_ctrl_list_mutex);
  1327. list_for_each_entry(bdev, &bif_ctrl_list, list) {
  1328. if (bdev->ctrl_dev && bdev->ctrl_dev->of_node == ctrl_node) {
  1329. bdev_found = bdev;
  1330. break;
  1331. }
  1332. }
  1333. mutex_unlock(&bif_ctrl_list_mutex);
  1334. if (bdev_found) {
  1335. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  1336. if (!ctrl) {
  1337. pr_err("Bus handle allocation failed\n");
  1338. ctrl = ERR_PTR(-ENOMEM);
  1339. } else {
  1340. ctrl->bdev = bdev_found;
  1341. }
  1342. }
  1343. return ctrl;
  1344. }
  1345. EXPORT_SYMBOL(bif_ctrl_get);
  1346. /**
  1347. * bif_ctrl_put() - frees a BIF controller handle
  1348. * @ctrl: BIF controller consumer handle
  1349. */
  1350. void bif_ctrl_put(struct bif_ctrl *ctrl)
  1351. {
  1352. if (!IS_ERR_OR_NULL(ctrl) && ctrl->exclusive_lock)
  1353. mutex_unlock(&ctrl->bdev->mutex);
  1354. kfree(ctrl);
  1355. }
  1356. EXPORT_SYMBOL(bif_ctrl_put);
  1357. static bool bif_slave_object_match(const struct bif_object *object,
  1358. const struct bif_match_criteria *criteria)
  1359. {
  1360. return (object->type == criteria->obj_type)
  1361. && (object->version == criteria->obj_version
  1362. || !(criteria->match_mask & BIF_MATCH_OBJ_VERSION))
  1363. && (object->manufacturer_id == criteria->obj_manufacturer_id
  1364. || !(criteria->match_mask & BIF_MATCH_OBJ_MANUFACTURER_ID));
  1365. }
  1366. /*
  1367. * Returns true if all parameters are matched, otherwise false.
  1368. * function_type and function_version mean that their exists some function in
  1369. * the slave which has the specified type and subtype. ctrl == NULL is treated
  1370. * as a wildcard.
  1371. */
  1372. static bool bif_slave_match(struct bif_ctrl *ctrl,
  1373. struct bif_slave_dev *sdev, const struct bif_match_criteria *criteria)
  1374. {
  1375. int i, type, version;
  1376. struct bif_object *object;
  1377. bool function_found = false;
  1378. bool object_found = false;
  1379. if (ctrl && (ctrl->bdev != sdev->bdev))
  1380. return false;
  1381. if (!sdev->present
  1382. && (!(criteria->match_mask & BIF_MATCH_IGNORE_PRESENCE)
  1383. || ((criteria->match_mask & BIF_MATCH_IGNORE_PRESENCE)
  1384. && !criteria->ignore_presence)))
  1385. return false;
  1386. if ((criteria->match_mask & BIF_MATCH_MANUFACTURER_ID)
  1387. && sdev->l1_data.manufacturer_id != criteria->manufacturer_id)
  1388. return false;
  1389. if ((criteria->match_mask & BIF_MATCH_PRODUCT_ID)
  1390. && sdev->l1_data.product_id != criteria->product_id)
  1391. return false;
  1392. if (criteria->match_mask & BIF_MATCH_FUNCTION_TYPE) {
  1393. if (!sdev->function_directory)
  1394. return false;
  1395. for (i = 0; i < sdev->l1_data.length / 4; i++) {
  1396. type = sdev->function_directory[i].function_type;
  1397. version = sdev->function_directory[i].function_version;
  1398. if (type == criteria->function_type &&
  1399. (version == criteria->function_version
  1400. || !(criteria->match_mask
  1401. & BIF_MATCH_FUNCTION_VERSION))) {
  1402. function_found = true;
  1403. break;
  1404. }
  1405. }
  1406. if (!function_found)
  1407. return false;
  1408. }
  1409. if (criteria->match_mask & BIF_MATCH_OBJ_TYPE) {
  1410. if (!sdev->nvm_function)
  1411. return false;
  1412. bif_ctrl_lock(ctrl);
  1413. list_for_each_entry(object, &sdev->nvm_function->object_list,
  1414. list) {
  1415. if (bif_slave_object_match(object, criteria)) {
  1416. object_found = true;
  1417. break;
  1418. }
  1419. }
  1420. bif_ctrl_unlock(ctrl);
  1421. if (!object_found)
  1422. return false;
  1423. }
  1424. return true;
  1425. }
  1426. /**
  1427. * bif_slave_match_count() - returns the number of slaves associated with the
  1428. * specified BIF controller which fit the matching
  1429. * criteria
  1430. * @ctrl: BIF controller consumer handle
  1431. * @match_criteria: Matching criteria used to filter slaves
  1432. */
  1433. int bif_slave_match_count(struct bif_ctrl *ctrl,
  1434. const struct bif_match_criteria *match_criteria)
  1435. {
  1436. struct bif_slave_dev *sdev;
  1437. int count = 0;
  1438. mutex_lock(&bif_sdev_list_mutex);
  1439. list_for_each_entry(sdev, &bif_sdev_list, list) {
  1440. if (bif_slave_match(ctrl, sdev, match_criteria))
  1441. count++;
  1442. }
  1443. mutex_unlock(&bif_sdev_list_mutex);
  1444. return count;
  1445. }
  1446. EXPORT_SYMBOL(bif_slave_match_count);
  1447. /**
  1448. * bif_slave_match_get() - get a slave handle for the id'th slave associated
  1449. * with the specified BIF controller which fits the
  1450. * matching criteria
  1451. * @ctrl: BIF controller consumer handle
  1452. * @id: Index into the set of matching slaves
  1453. * @match_criteria: Matching criteria used to filter slaves
  1454. *
  1455. * id must be in the range [0, bif_slave_match_count(ctrl, match_criteria) - 1].
  1456. *
  1457. * Returns a BIF slave handle if successful or an ERR_PTR if not.
  1458. */
  1459. struct bif_slave *bif_slave_match_get(struct bif_ctrl *ctrl,
  1460. unsigned int id, const struct bif_match_criteria *match_criteria)
  1461. {
  1462. struct bif_slave_dev *sdev;
  1463. struct bif_slave *slave = ERR_PTR(-ENODEV);
  1464. struct bif_slave_dev *sdev_found = NULL;
  1465. int count = 0;
  1466. mutex_lock(&bif_sdev_list_mutex);
  1467. list_for_each_entry(sdev, &bif_sdev_list, list) {
  1468. if (bif_slave_match(ctrl, sdev, match_criteria))
  1469. count++;
  1470. if (count == id + 1) {
  1471. sdev_found = sdev;
  1472. break;
  1473. }
  1474. }
  1475. mutex_unlock(&bif_sdev_list_mutex);
  1476. if (sdev_found) {
  1477. slave = kzalloc(sizeof(*slave), GFP_KERNEL);
  1478. if (!slave) {
  1479. pr_err("Slave allocation failed\n");
  1480. slave = ERR_PTR(-ENOMEM);
  1481. } else {
  1482. slave->sdev = sdev_found;
  1483. slave->ctrl.bdev = sdev_found->bdev;
  1484. }
  1485. }
  1486. return slave;
  1487. }
  1488. EXPORT_SYMBOL(bif_slave_match_get);
  1489. /**
  1490. * bif_slave_put() - frees a BIF slave handle
  1491. * @slave: BIF slave handle
  1492. */
  1493. void bif_slave_put(struct bif_slave *slave)
  1494. {
  1495. if (!IS_ERR_OR_NULL(slave) && slave->ctrl.exclusive_lock)
  1496. mutex_unlock(&slave->sdev->bdev->mutex);
  1497. kfree(slave);
  1498. }
  1499. EXPORT_SYMBOL(bif_slave_put);
  1500. /**
  1501. * bif_slave_find_function() - get the function pointer and version of a
  1502. * BIF function if it is present on the specified slave
  1503. * @slave: BIF slave handle
  1504. * @function: BIF function to search for inside of the slave
  1505. * @version: If the function is found, then 'version' is set to the
  1506. * version value of the function
  1507. * @function_pointer: If the function is found, then 'function_pointer' is set
  1508. * to the BIF slave address of the function
  1509. *
  1510. * Returns 0 for success or errno if an error occurred. If the function is not
  1511. * found in the slave, then -ENODEV is returned.
  1512. */
  1513. int bif_slave_find_function(struct bif_slave *slave, u8 function, u8 *version,
  1514. u16 *function_pointer)
  1515. {
  1516. int rc = -ENODEV;
  1517. struct bif_ddb_l2_data *func;
  1518. int i;
  1519. if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(version)
  1520. || IS_ERR_OR_NULL(function_pointer)) {
  1521. pr_err("Invalid pointer input.\n");
  1522. return -EINVAL;
  1523. }
  1524. func = slave->sdev->function_directory;
  1525. for (i = 0; i < slave->sdev->l1_data.length / 4; i++) {
  1526. if (function == func[i].function_type) {
  1527. *version = func[i].function_version;
  1528. *function_pointer = func[i].function_pointer;
  1529. rc = 0;
  1530. break;
  1531. }
  1532. }
  1533. return rc;
  1534. }
  1535. EXPORT_SYMBOL(bif_slave_find_function);
  1536. static bool bif_object_match(const struct bif_object *object,
  1537. const struct bif_obj_match_criteria *criteria)
  1538. {
  1539. return (object->type == criteria->type
  1540. || !(criteria->match_mask & BIF_OBJ_MATCH_TYPE))
  1541. && (object->version == criteria->version
  1542. || !(criteria->match_mask & BIF_OBJ_MATCH_VERSION))
  1543. && (object->manufacturer_id == criteria->manufacturer_id
  1544. || !(criteria->match_mask & BIF_OBJ_MATCH_MANUFACTURER_ID));
  1545. }
  1546. /**
  1547. * bif_object_match_count() - returns the number of objects associated with the
  1548. * specified BIF slave which fit the matching criteria
  1549. * @slave: BIF slave handle
  1550. * @match_criteria: Matching criteria used to filter objects
  1551. */
  1552. int bif_object_match_count(struct bif_slave *slave,
  1553. const struct bif_obj_match_criteria *match_criteria)
  1554. {
  1555. struct bif_object *object;
  1556. int count = 0;
  1557. if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(match_criteria)) {
  1558. pr_err("Invalid pointer input.\n");
  1559. return -EINVAL;
  1560. }
  1561. if (!slave->sdev->nvm_function)
  1562. return 0;
  1563. bif_slave_ctrl_lock(slave);
  1564. list_for_each_entry(object, &slave->sdev->nvm_function->object_list,
  1565. list) {
  1566. if (bif_object_match(object, match_criteria))
  1567. count++;
  1568. }
  1569. bif_slave_ctrl_unlock(slave);
  1570. return count;
  1571. }
  1572. EXPORT_SYMBOL(bif_object_match_count);
  1573. /**
  1574. * bif_object_match_get() - get a BIF object handle for the id'th object found
  1575. * in the non-volatile memory of the specified BIF slave
  1576. * which fits the matching criteria
  1577. * @slave: BIF slave handle
  1578. * @id: Index into the set of matching objects
  1579. * @match_criteria: Matching criteria used to filter objects
  1580. *
  1581. * id must be in range [0, bif_object_match_count(slave, match_criteria) - 1].
  1582. *
  1583. * Returns a BIF object handle if successful or an ERR_PTR if not. This handle
  1584. * must be freed using bif_object_put() when it is no longer needed.
  1585. */
  1586. struct bif_object *bif_object_match_get(struct bif_slave *slave,
  1587. unsigned int id, const struct bif_obj_match_criteria *match_criteria)
  1588. {
  1589. struct bif_object *object;
  1590. struct bif_object *object_found = NULL;
  1591. struct bif_object *object_consumer = ERR_PTR(-ENODEV);
  1592. int count = 0;
  1593. if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(match_criteria)) {
  1594. pr_err("Invalid pointer input.\n");
  1595. return ERR_PTR(-EINVAL);
  1596. }
  1597. if (!slave->sdev->nvm_function)
  1598. return object_consumer;
  1599. bif_slave_ctrl_lock(slave);
  1600. list_for_each_entry(object, &slave->sdev->nvm_function->object_list,
  1601. list) {
  1602. if (bif_object_match(object, match_criteria))
  1603. count++;
  1604. if (count == id + 1) {
  1605. object_found = object;
  1606. break;
  1607. }
  1608. }
  1609. if (object_found) {
  1610. object_consumer = kmemdup(object_found,
  1611. sizeof(*object_consumer), GFP_KERNEL);
  1612. if (!object_consumer) {
  1613. pr_err("out of memory\n");
  1614. object_consumer = ERR_PTR(-ENOMEM);
  1615. goto done;
  1616. }
  1617. object_consumer->data = kmemdup(object_found->data,
  1618. object_found->length - 8, GFP_KERNEL);
  1619. if (!object_consumer->data) {
  1620. pr_err("out of memory\n");
  1621. kfree(object_consumer);
  1622. object_consumer = ERR_PTR(-ENOMEM);
  1623. goto done;
  1624. }
  1625. /*
  1626. * Use prev pointer in consumer struct to point to original
  1627. * struct in the internal linked list.
  1628. */
  1629. object_consumer->list.prev = &object_found->list;
  1630. }
  1631. done:
  1632. bif_slave_ctrl_unlock(slave);
  1633. return object_consumer;
  1634. }
  1635. EXPORT_SYMBOL(bif_object_match_get);
  1636. /**
  1637. * bif_object_put() - frees the memory allocated for a BIF object pointer
  1638. * returned by bif_object_match_get()
  1639. * @object: BIF object to free
  1640. */
  1641. void bif_object_put(struct bif_object *object)
  1642. {
  1643. if (object)
  1644. kfree(object->data);
  1645. kfree(object);
  1646. }
  1647. EXPORT_SYMBOL(bif_object_put);
  1648. /* Copies the contents of object into buf following MIPI-BIF formatting. */
  1649. static void bif_object_flatten(u8 *buf, const struct bif_object *object)
  1650. {
  1651. buf[0] = object->type;
  1652. buf[1] = object->version;
  1653. buf[2] = object->manufacturer_id >> 8;
  1654. buf[3] = object->manufacturer_id;
  1655. buf[4] = object->length >> 8;
  1656. buf[5] = object->length;
  1657. memcpy(&buf[6], object->data, object->length - 8);
  1658. buf[object->length - 2] = object->crc >> 8;
  1659. buf[object->length - 1] = object->crc;
  1660. }
  1661. /**
  1662. * bif_object_write() - writes a new BIF object at the end of the object list in
  1663. * the non-volatile memory of a slave
  1664. * @slave: BIF slave handle
  1665. * @type: Type of the object
  1666. * @version: Version of the object
  1667. * @manufacturer_id: Manufacturer ID number allocated by MIPI
  1668. * @data: Data contained in the object
  1669. * @data_len: Length of the data
  1670. *
  1671. * Returns 0 on success or errno on failure. This function will fail if the NVM
  1672. * lock points to an offset after the BIF object list terminator (0x00).
  1673. */
  1674. int bif_object_write(struct bif_slave *slave, u8 type, u8 version,
  1675. u16 manufacturer_id, const u8 *data, int data_len)
  1676. {
  1677. struct bif_object *object;
  1678. struct bif_object *tail_object;
  1679. struct bif_nvm_function *nvm;
  1680. int rc;
  1681. int add_null = 0;
  1682. u16 offset = 0;
  1683. u8 *buf;
  1684. if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(data)) {
  1685. pr_err("Invalid input pointer\n");
  1686. return -EINVAL;
  1687. }
  1688. nvm = slave->sdev->nvm_function;
  1689. if (!nvm) {
  1690. pr_err("BIF slave has no NVM function\n");
  1691. return -ENODEV;
  1692. }
  1693. bif_slave_ctrl_lock(slave);
  1694. if (nvm->object_count > 0) {
  1695. tail_object = list_entry(nvm->object_list.prev,
  1696. struct bif_object, list);
  1697. offset = tail_object->addr - nvm->nvm_base_address
  1698. + tail_object->length;
  1699. }
  1700. if (offset < nvm->nvm_lock_offset) {
  1701. pr_err("Cannot write BIF object to NVM because the end of the object list is locked (end=%d < lock=%d)\n",
  1702. offset, nvm->nvm_lock_offset);
  1703. rc = -EPERM;
  1704. goto error_unlock;
  1705. } else if (offset + data_len + 8 > nvm->nvm_size) {
  1706. pr_err("Cannot write BIF object to NVM because there is not enough remaining space (size=%d > remaining=%d)\n",
  1707. data_len + 8, nvm->nvm_size - offset);
  1708. rc = -EINVAL;
  1709. goto error_unlock;
  1710. }
  1711. if (offset + data_len + 8 < nvm->nvm_size)
  1712. add_null = 1;
  1713. object = kzalloc(sizeof(*object), GFP_KERNEL);
  1714. if (!object) {
  1715. pr_err("kzalloc failed\n");
  1716. rc = -ENOMEM;
  1717. goto error_unlock;
  1718. }
  1719. object->data = kzalloc(data_len, GFP_KERNEL);
  1720. if (!object->data) {
  1721. pr_err("kzalloc failed\n");
  1722. rc = -ENOMEM;
  1723. goto free_object;
  1724. }
  1725. buf = kzalloc(data_len + 8 + add_null, GFP_KERNEL);
  1726. if (!buf) {
  1727. pr_err("kzalloc failed\n");
  1728. rc = -ENOMEM;
  1729. goto free_data;
  1730. }
  1731. object->type = type;
  1732. object->version = version;
  1733. object->manufacturer_id = manufacturer_id;
  1734. object->length = data_len + 8;
  1735. memcpy(object->data, data, data_len);
  1736. object->crc = bif_object_crc_ccitt(object);
  1737. object->addr = offset + nvm->nvm_base_address;
  1738. bif_object_flatten(buf, object);
  1739. if (add_null)
  1740. buf[object->length] = BIF_OBJ_END_OF_LIST;
  1741. rc = _bif_slave_nvm_raw_write(slave->sdev, offset, buf,
  1742. object->length + add_null);
  1743. if (rc < 0) {
  1744. pr_err("NVM write failed, rc=%d\n", rc);
  1745. kfree(buf);
  1746. goto free_data;
  1747. }
  1748. kfree(buf);
  1749. list_add_tail(&object->list, &nvm->object_list);
  1750. nvm->object_count++;
  1751. bif_slave_ctrl_unlock(slave);
  1752. return rc;
  1753. free_data:
  1754. kfree(object->data);
  1755. free_object:
  1756. kfree(object);
  1757. error_unlock:
  1758. bif_slave_ctrl_unlock(slave);
  1759. return rc;
  1760. }
  1761. EXPORT_SYMBOL(bif_object_write);
  1762. /*
  1763. * Returns a pointer to the internal object referenced by a consumer object
  1764. * if it exists. Returns NULL if the internal object cannot be found.
  1765. */
  1766. static struct bif_object *bif_object_consumer_search(
  1767. struct bif_nvm_function *nvm, const struct bif_object *consumer_object)
  1768. {
  1769. struct bif_object *object = NULL;
  1770. struct bif_object *search_object;
  1771. /*
  1772. * Internal struct in object linked list is pointed to by consumer
  1773. * object list.prev.
  1774. */
  1775. search_object = list_entry(consumer_object->list.prev,
  1776. struct bif_object, list);
  1777. list_for_each_entry(object, &nvm->object_list, list) {
  1778. if (object == search_object)
  1779. break;
  1780. }
  1781. if (object != search_object)
  1782. return NULL;
  1783. return object;
  1784. }
  1785. /**
  1786. * bif_object_overwrite() - overwrites an existing BIF object found in the
  1787. * non-volatile memory of a slave
  1788. * @slave: BIF slave handle
  1789. * @object: Existing object in the slave to overwrite
  1790. * @type: Type of the object
  1791. * @version: Version of the object
  1792. * @manufacturer_id: Manufacturer ID number allocated by MIPI
  1793. * @data: Data contained in the object
  1794. * @data_len: Length of the data
  1795. *
  1796. * Returns 0 on success or errno on failure. The data stored within 'object'
  1797. * is updated to the new values upon success. The new data written to the
  1798. * object must have exactly the same length as the old data (i.e.
  1799. * data_len == object->length - 8).
  1800. *
  1801. * This function will fail if the NVM lock points to an offset after the
  1802. * beginning of the existing BIF object.
  1803. */
  1804. int bif_object_overwrite(struct bif_slave *slave,
  1805. struct bif_object *object, u8 type, u8 version,
  1806. u16 manufacturer_id, const u8 *data, int data_len)
  1807. {
  1808. struct bif_object *edit_object = NULL;
  1809. struct bif_nvm_function *nvm;
  1810. int rc;
  1811. u16 crc;
  1812. u8 *buf;
  1813. if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(object)
  1814. || IS_ERR_OR_NULL(data)) {
  1815. pr_err("Invalid input pointer\n");
  1816. return -EINVAL;
  1817. }
  1818. nvm = slave->sdev->nvm_function;
  1819. if (!nvm) {
  1820. pr_err("BIF slave has no NVM function\n");
  1821. return -ENODEV;
  1822. }
  1823. if (data_len + 8 != object->length) {
  1824. pr_err("New data length=%d is different from existing length=%d\n",
  1825. data_len, object->length - 8);
  1826. return -EINVAL;
  1827. }
  1828. bif_slave_ctrl_lock(slave);
  1829. edit_object = bif_object_consumer_search(nvm, object);
  1830. if (!edit_object) {
  1831. pr_err("BIF object not found within slave\n");
  1832. rc = -EINVAL;
  1833. goto error_unlock;
  1834. }
  1835. if (edit_object->addr - nvm->nvm_base_address < nvm->nvm_lock_offset) {
  1836. pr_err("Cannot overwrite BIF object in NVM because some portion of it is locked\n");
  1837. rc = -EPERM;
  1838. goto error_unlock;
  1839. }
  1840. buf = kzalloc(data_len + 8, GFP_KERNEL);
  1841. if (!buf) {
  1842. pr_err("kzalloc failed\n");
  1843. rc = -ENOMEM;
  1844. goto error_unlock;
  1845. }
  1846. buf[0] = type;
  1847. buf[1] = version;
  1848. buf[2] = manufacturer_id >> 8;
  1849. buf[3] = manufacturer_id;
  1850. buf[4] = (data_len + 8) >> 8;
  1851. buf[5] = data_len + 8;
  1852. memcpy(&buf[6], data, data_len);
  1853. crc = bif_crc_ccitt(buf, data_len + 6);
  1854. buf[data_len + 6] = crc >> 8;
  1855. buf[data_len + 7] = crc;
  1856. rc = _bif_slave_nvm_raw_write(slave->sdev,
  1857. object->addr - nvm->nvm_base_address, buf, data_len + 8);
  1858. if (rc < 0) {
  1859. pr_err("NVM write failed, rc=%d\n", rc);
  1860. kfree(buf);
  1861. goto error_unlock;
  1862. }
  1863. kfree(buf);
  1864. /* Update internal object struct. */
  1865. edit_object->type = type;
  1866. edit_object->version = version;
  1867. edit_object->manufacturer_id = manufacturer_id;
  1868. edit_object->length = data_len + 8;
  1869. memcpy(edit_object->data, data, data_len);
  1870. edit_object->crc = crc;
  1871. /* Update consumer object struct. */
  1872. object->type = type;
  1873. object->version = version;
  1874. object->manufacturer_id = manufacturer_id;
  1875. object->length = data_len + 8;
  1876. memcpy(object->data, data, data_len);
  1877. object->crc = crc;
  1878. error_unlock:
  1879. bif_slave_ctrl_unlock(slave);
  1880. return rc;
  1881. }
  1882. EXPORT_SYMBOL(bif_object_overwrite);
  1883. /**
  1884. * bif_object_delete() - deletes an existing BIF object found in the
  1885. * non-volatile memory of a slave. Objects found in the
  1886. * object list in the NVM of the slave are shifted forward
  1887. * in order to fill the hole left by the deleted object
  1888. * @slave: BIF slave handle
  1889. * @object: Existing object in the slave to delete
  1890. *
  1891. * Returns 0 on success or errno on failure. bif_object_put() must still be
  1892. * called after this function in order to free the memory in the consumer
  1893. * 'object' struct pointer.
  1894. *
  1895. * This function will fail if the NVM lock points to an offset after the
  1896. * beginning of the existing BIF object.
  1897. */
  1898. int bif_object_delete(struct bif_slave *slave, const struct bif_object *object)
  1899. {
  1900. struct bif_object *del_object = NULL;
  1901. struct bif_object *tail_object;
  1902. struct bif_nvm_function *nvm;
  1903. bool found = false;
  1904. int pos = 0;
  1905. int rc;
  1906. u8 *buf;
  1907. if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(object)) {
  1908. pr_err("Invalid input pointer\n");
  1909. return -EINVAL;
  1910. }
  1911. nvm = slave->sdev->nvm_function;
  1912. if (!nvm) {
  1913. pr_err("BIF slave has no NVM function\n");
  1914. return -ENODEV;
  1915. }
  1916. bif_slave_ctrl_lock(slave);
  1917. del_object = bif_object_consumer_search(nvm, object);
  1918. if (!del_object) {
  1919. pr_err("BIF object not found within slave\n");
  1920. rc = -EINVAL;
  1921. goto error_unlock;
  1922. }
  1923. if (del_object->addr - nvm->nvm_base_address < nvm->nvm_lock_offset) {
  1924. pr_err("Cannot delete BIF object in NVM because some portion of it is locked\n");
  1925. rc = -EPERM;
  1926. goto error_unlock;
  1927. }
  1928. buf = kmalloc(nvm->nvm_size, GFP_KERNEL);
  1929. if (!buf) {
  1930. pr_err("kzalloc failed\n");
  1931. rc = -ENOMEM;
  1932. goto error_unlock;
  1933. }
  1934. /*
  1935. * Copy the contents of objects after the one to be deleted into a flat
  1936. * array.
  1937. */
  1938. list_for_each_entry(tail_object, &nvm->object_list, list) {
  1939. if (found) {
  1940. bif_object_flatten(&buf[pos], tail_object);
  1941. pos += tail_object->length;
  1942. } else if (tail_object == del_object) {
  1943. found = true;
  1944. }
  1945. }
  1946. /* Add the list terminator. */
  1947. buf[pos++] = BIF_OBJ_END_OF_LIST;
  1948. rc = _bif_slave_nvm_raw_write(slave->sdev,
  1949. del_object->addr - nvm->nvm_base_address, buf, pos);
  1950. if (rc < 0) {
  1951. pr_err("NVM write failed, rc=%d\n", rc);
  1952. kfree(buf);
  1953. goto error_unlock;
  1954. }
  1955. kfree(buf);
  1956. /* Update the addresses of the objects after the one to be deleted. */
  1957. found = false;
  1958. list_for_each_entry(tail_object, &nvm->object_list, list) {
  1959. if (found)
  1960. tail_object->addr -= del_object->length;
  1961. else if (tail_object == del_object)
  1962. found = true;
  1963. }
  1964. list_del(&del_object->list);
  1965. kfree(del_object->data);
  1966. kfree(del_object);
  1967. nvm->object_count--;
  1968. error_unlock:
  1969. bif_slave_ctrl_unlock(slave);
  1970. return rc;
  1971. }
  1972. EXPORT_SYMBOL(bif_object_delete);
  1973. /**
  1974. * bif_slave_read() - read contiguous memory values from a BIF slave
  1975. * @slave: BIF slave handle
  1976. * @addr: BIF slave address to begin reading at
  1977. * @buf: Buffer to fill with memory values
  1978. * @len: Number of byte to read
  1979. *
  1980. * Returns 0 for success or errno if an error occurred.
  1981. */
  1982. int bif_slave_read(struct bif_slave *slave, u16 addr, u8 *buf, int len)
  1983. {
  1984. int rc;
  1985. if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(buf)) {
  1986. pr_err("Invalid pointer input.\n");
  1987. return -EINVAL;
  1988. }
  1989. bif_slave_ctrl_lock(slave);
  1990. rc = _bif_slave_read(slave->sdev, addr, buf, len);
  1991. if (rc)
  1992. pr_err("BIF slave read failed, rc=%d\n", rc);
  1993. bif_slave_ctrl_unlock(slave);
  1994. return rc;
  1995. }
  1996. EXPORT_SYMBOL(bif_slave_read);
  1997. /**
  1998. * bif_slave_write() - write contiguous memory values to a BIF slave
  1999. * @slave: BIF slave handle
  2000. * @addr: BIF slave address to begin writing at
  2001. * @buf: Buffer containing values to write
  2002. * @len: Number of byte to write
  2003. *
  2004. * Returns 0 for success or errno if an error occurred.
  2005. */
  2006. int bif_slave_write(struct bif_slave *slave, u16 addr, u8 *buf, int len)
  2007. {
  2008. int rc;
  2009. if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(buf)) {
  2010. pr_err("Invalid pointer input.\n");
  2011. return -EINVAL;
  2012. }
  2013. bif_slave_ctrl_lock(slave);
  2014. rc = _bif_slave_write(slave->sdev, addr, buf, len);
  2015. if (rc)
  2016. pr_err("BIF slave write failed, rc=%d\n", rc);
  2017. bif_slave_ctrl_unlock(slave);
  2018. return rc;
  2019. }
  2020. EXPORT_SYMBOL(bif_slave_write);
  2021. /**
  2022. * bif_slave_nvm_raw_read() - read contiguous memory values from a BIF slave's
  2023. * non-volatile memory (NVM)
  2024. * @slave: BIF slave handle
  2025. * @offset: Offset from the beginning of BIF slave NVM to begin reading at
  2026. * @buf: Buffer to fill with memory values
  2027. * @len: Number of byte to read
  2028. *
  2029. * Returns 0 for success or errno if an error occurred.
  2030. */
  2031. int bif_slave_nvm_raw_read(struct bif_slave *slave, u16 offset, u8 *buf,
  2032. int len)
  2033. {
  2034. if (IS_ERR_OR_NULL(slave)) {
  2035. pr_err("Invalid slave pointer=%ld\n", PTR_ERR(slave));
  2036. return -EINVAL;
  2037. } else if (IS_ERR_OR_NULL(buf)) {
  2038. pr_err("Invalid buffer pointer=%ld\n", PTR_ERR(buf));
  2039. return -EINVAL;
  2040. } else if (!slave->sdev->nvm_function) {
  2041. pr_err("BIF slave has no NVM function\n");
  2042. return -ENODEV;
  2043. } else if (offset + len > slave->sdev->nvm_function->nvm_size) {
  2044. pr_err("read offset + len = %d > NVM size = %d\n",
  2045. offset + len, slave->sdev->nvm_function->nvm_size);
  2046. return -EINVAL;
  2047. }
  2048. return bif_slave_read(slave,
  2049. slave->sdev->nvm_function->nvm_base_address + offset, buf, len);
  2050. }
  2051. EXPORT_SYMBOL(bif_slave_nvm_raw_read);
  2052. /**
  2053. * bif_slave_nvm_raw_write() - write contiguous memory values to a BIF slave's
  2054. * non-volatile memory (NVM)
  2055. * @slave: BIF slave handle
  2056. * @offset: Offset from the beginning of BIF slave NVM to begin writing at
  2057. * @buf: Buffer containing values to write
  2058. * @len: Number of byte to write
  2059. *
  2060. * Note that this function does *not* respect the MIPI-BIF object data
  2061. * formatting specification. It can cause corruption of the object data list
  2062. * stored in NVM if used improperly.
  2063. *
  2064. * Returns 0 for success or errno if an error occurred.
  2065. */
  2066. int bif_slave_nvm_raw_write(struct bif_slave *slave, u16 offset, u8 *buf,
  2067. int len)
  2068. {
  2069. int rc;
  2070. if (IS_ERR_OR_NULL(slave)) {
  2071. pr_err("Invalid slave pointer=%ld\n", PTR_ERR(slave));
  2072. return -EINVAL;
  2073. } else if (IS_ERR_OR_NULL(buf)) {
  2074. pr_err("Invalid buffer pointer=%ld\n", PTR_ERR(buf));
  2075. return -EINVAL;
  2076. }
  2077. bif_slave_ctrl_lock(slave);
  2078. rc = _bif_slave_nvm_raw_write(slave->sdev, offset, buf, len);
  2079. bif_slave_ctrl_unlock(slave);
  2080. return rc;
  2081. }
  2082. EXPORT_SYMBOL(bif_slave_nvm_raw_write);
  2083. /**
  2084. * bif_slave_is_present() - check if a slave is currently physically present
  2085. * in the system
  2086. * @slave: BIF slave handle
  2087. *
  2088. * Returns 1 if the slave is present, 0 if the slave is not present, or errno
  2089. * if an error occurred.
  2090. *
  2091. * This function can be used by BIF consumer drivers to check if their slave
  2092. * handles are still meaningful after battery reinsertion.
  2093. */
  2094. int bif_slave_is_present(struct bif_slave *slave)
  2095. {
  2096. if (IS_ERR_OR_NULL(slave)) {
  2097. pr_err("Invalid pointer input.\n");
  2098. return -EINVAL;
  2099. }
  2100. return slave->sdev->present;
  2101. }
  2102. EXPORT_SYMBOL(bif_slave_is_present);
  2103. /**
  2104. * bif_slave_is_selected() - check if a slave is currently selected on the BIF
  2105. * bus
  2106. * @slave: BIF slave handle
  2107. *
  2108. * Returns 1 if the slave is selected, 0 if the slave is not selected, or errno
  2109. * if an error occurred.
  2110. *
  2111. * This function should not be required under normal circumstances since the
  2112. * bif-core framework ensures that slaves are always selected when needed.
  2113. * It would be most useful when used as a helper in conjunction with
  2114. * bif_ctrl_bus_lock() and the raw transaction functions.
  2115. */
  2116. int bif_slave_is_selected(struct bif_slave *slave)
  2117. {
  2118. int rc;
  2119. if (IS_ERR_OR_NULL(slave)) {
  2120. pr_err("Invalid pointer input.\n");
  2121. return -EINVAL;
  2122. }
  2123. if (slave->sdev->bdev->selected_sdev != slave->sdev)
  2124. return false;
  2125. bif_slave_ctrl_lock(slave);
  2126. rc = bif_is_slave_selected(slave->sdev->bdev);
  2127. bif_slave_ctrl_unlock(slave);
  2128. return rc;
  2129. }
  2130. EXPORT_SYMBOL(bif_slave_is_selected);
  2131. /**
  2132. * bif_slave_select() - select a slave on the BIF bus
  2133. * @slave: BIF slave handle
  2134. *
  2135. * Returns 0 on success or errno if an error occurred.
  2136. *
  2137. * This function should not be required under normal circumstances since the
  2138. * bif-core framework ensures that slaves are always selected when needed.
  2139. * It would be most useful when used as a helper in conjunction with
  2140. * bif_ctrl_bus_lock() and the raw transaction functions.
  2141. */
  2142. int bif_slave_select(struct bif_slave *slave)
  2143. {
  2144. int rc;
  2145. if (IS_ERR_OR_NULL(slave)) {
  2146. pr_err("Invalid pointer input.\n");
  2147. return -EINVAL;
  2148. }
  2149. bif_slave_ctrl_lock(slave);
  2150. slave->sdev->bdev->selected_sdev = NULL;
  2151. rc = bif_select_slave(slave->sdev);
  2152. bif_slave_ctrl_unlock(slave);
  2153. return rc;
  2154. }
  2155. EXPORT_SYMBOL(bif_slave_select);
  2156. /**
  2157. * bif_ctrl_raw_transaction() - perform a raw BIF transaction on the bus which
  2158. * expects no slave response
  2159. * @ctrl: BIF controller consumer handle
  2160. * @transaction: BIF transaction to carry out. This should be one of the
  2161. * values in enum bif_transaction.
  2162. * @data: 8-bit data to use in the transaction. The meaning of
  2163. * this data depends upon the transaction that is to be
  2164. * performed.
  2165. *
  2166. * When performing a bus command (BC) transaction, values in enum
  2167. * bif_bus_command may be used for the data parameter. Additional manufacturer
  2168. * specific values may also be used in a BC transaction.
  2169. *
  2170. * Returns 0 on success or errno if an error occurred.
  2171. *
  2172. * This function should only need to be used when BIF transactions are required
  2173. * that are not handled by the bif-core directly.
  2174. */
  2175. int bif_ctrl_raw_transaction(struct bif_ctrl *ctrl, int transaction, u8 data)
  2176. {
  2177. int rc;
  2178. if (IS_ERR_OR_NULL(ctrl)) {
  2179. pr_err("Invalid pointer input.\n");
  2180. return -EINVAL;
  2181. }
  2182. bif_ctrl_lock(ctrl);
  2183. rc = ctrl->bdev->desc->ops->bus_transaction(ctrl->bdev, transaction,
  2184. data);
  2185. if (rc)
  2186. pr_err("BIF bus transaction failed, rc=%d\n", rc);
  2187. bif_ctrl_unlock(ctrl);
  2188. return rc;
  2189. }
  2190. EXPORT_SYMBOL(bif_ctrl_raw_transaction);
  2191. /**
  2192. * bif_ctrl_raw_transaction_read() - perform a raw BIF transaction on the bus
  2193. * which expects an RD or TACK slave response word
  2194. * @ctrl: BIF controller consumer handle
  2195. * @transaction: BIF transaction to carry out. This should be one of the
  2196. * values in enum bif_transaction.
  2197. * @data: 8-bit data to use in the transaction. The meaning of
  2198. * this data depends upon the transaction that is to be
  2199. * performed.
  2200. * @response: Pointer to an integer which is filled with the 11-bit
  2201. * slave response word upon success. The 11-bit format is
  2202. * (MSB to LSB) BCF, ACK, EOT, D7-D0.
  2203. *
  2204. * When performing a bus command (BC) transaction, values in enum
  2205. * bif_bus_command may be used for the data parameter. Additional manufacturer
  2206. * specific values may also be used in a BC transaction.
  2207. *
  2208. * Returns 0 on success or errno if an error occurred.
  2209. *
  2210. * This function should only need to be used when BIF transactions are required
  2211. * that are not handled by the bif-core directly.
  2212. */
  2213. int bif_ctrl_raw_transaction_read(struct bif_ctrl *ctrl, int transaction,
  2214. u8 data, int *response)
  2215. {
  2216. int rc;
  2217. if (IS_ERR_OR_NULL(ctrl) || IS_ERR_OR_NULL(response)) {
  2218. pr_err("Invalid pointer input.\n");
  2219. return -EINVAL;
  2220. }
  2221. bif_ctrl_lock(ctrl);
  2222. rc = ctrl->bdev->desc->ops->bus_transaction_read(ctrl->bdev,
  2223. transaction, data, response);
  2224. if (rc)
  2225. pr_err("BIF bus transaction failed, rc=%d\n", rc);
  2226. bif_ctrl_unlock(ctrl);
  2227. return rc;
  2228. }
  2229. EXPORT_SYMBOL(bif_ctrl_raw_transaction_read);
  2230. /**
  2231. * bif_ctrl_raw_transaction_query() - perform a raw BIF transaction on the bus
  2232. * which expects a BQ slave response
  2233. * @ctrl: BIF controller consumer handle
  2234. * @transaction: BIF transaction to carry out. This should be one of the
  2235. * values in enum bif_transaction.
  2236. * @data: 8-bit data to use in the transaction. The meaning of
  2237. * this data depends upon the transaction that is to be
  2238. * performed.
  2239. * @query_response: Pointer to boolean which is set to true if a BQ pulse
  2240. * is receieved, or false if no BQ pulse is received before
  2241. * timing out.
  2242. *
  2243. * When performing a bus command (BC) transaction, values in enum
  2244. * bif_bus_command may be used for the data parameter. Additional manufacturer
  2245. * specific values may also be used in a BC transaction.
  2246. *
  2247. * Returns 0 on success or errno if an error occurred.
  2248. *
  2249. * This function should only need to be used when BIF transactions are required
  2250. * that are not handled by the bif-core directly.
  2251. */
  2252. int bif_ctrl_raw_transaction_query(struct bif_ctrl *ctrl, int transaction,
  2253. u8 data, bool *query_response)
  2254. {
  2255. int rc;
  2256. if (IS_ERR_OR_NULL(ctrl) || IS_ERR_OR_NULL(query_response)) {
  2257. pr_err("Invalid pointer input.\n");
  2258. return -EINVAL;
  2259. }
  2260. bif_ctrl_lock(ctrl);
  2261. rc = ctrl->bdev->desc->ops->bus_transaction_query(ctrl->bdev,
  2262. transaction, data, query_response);
  2263. if (rc)
  2264. pr_err("BIF bus transaction failed, rc=%d\n", rc);
  2265. bif_ctrl_unlock(ctrl);
  2266. return rc;
  2267. }
  2268. EXPORT_SYMBOL(bif_ctrl_raw_transaction_query);
  2269. /**
  2270. * bif_ctrl_bus_lock() - lock the BIF bus of a controller for exclusive access
  2271. * @ctrl: BIF controller consumer handle
  2272. *
  2273. * This function should only need to be called in circumstances where a BIF
  2274. * consumer is issuing special BIF bus commands that have strict ordering
  2275. * requirements.
  2276. */
  2277. void bif_ctrl_bus_lock(struct bif_ctrl *ctrl)
  2278. {
  2279. if (IS_ERR_OR_NULL(ctrl)) {
  2280. pr_err("Invalid controller handle.\n");
  2281. return;
  2282. }
  2283. if (ctrl->exclusive_lock) {
  2284. pr_err("BIF bus exclusive lock already held\n");
  2285. return;
  2286. }
  2287. mutex_lock(&ctrl->bdev->mutex);
  2288. ctrl->exclusive_lock = true;
  2289. bif_cancel_irq_mode_work(ctrl->bdev);
  2290. }
  2291. EXPORT_SYMBOL(bif_ctrl_bus_lock);
  2292. /**
  2293. * bif_ctrl_bus_unlock() - lock the BIF bus of a controller that was previously
  2294. * locked for exclusive access
  2295. * @ctrl: BIF controller consumer handle
  2296. *
  2297. * This function must only be called after first calling bif_ctrl_bus_lock().
  2298. */
  2299. void bif_ctrl_bus_unlock(struct bif_ctrl *ctrl)
  2300. {
  2301. if (IS_ERR_OR_NULL(ctrl)) {
  2302. pr_err("Invalid controller handle.\n");
  2303. return;
  2304. }
  2305. if (!ctrl->exclusive_lock) {
  2306. pr_err("BIF bus exclusive lock not already held\n");
  2307. return;
  2308. }
  2309. ctrl->exclusive_lock = false;
  2310. bif_schedule_irq_mode_work(ctrl->bdev);
  2311. mutex_unlock(&ctrl->bdev->mutex);
  2312. }
  2313. EXPORT_SYMBOL(bif_ctrl_bus_unlock);
  2314. /**
  2315. * bif_ctrl_measure_rid() - measure the battery pack Rid pull-down resistance
  2316. * in ohms
  2317. * @ctrl: BIF controller consumer handle
  2318. *
  2319. * Returns the resistance of the Rid resistor in ohms if successful or errno
  2320. * if an error occurred.
  2321. */
  2322. int bif_ctrl_measure_rid(struct bif_ctrl *ctrl)
  2323. {
  2324. int rc;
  2325. if (IS_ERR_OR_NULL(ctrl)) {
  2326. pr_err("Invalid controller handle.\n");
  2327. return -ENODEV;
  2328. }
  2329. if (!ctrl->bdev->desc->ops->get_battery_rid) {
  2330. pr_err("Cannot measure Rid.\n");
  2331. return -ENXIO;
  2332. }
  2333. bif_ctrl_lock(ctrl);
  2334. rc = ctrl->bdev->desc->ops->get_battery_rid(ctrl->bdev);
  2335. if (rc < 0)
  2336. pr_err("Error during Rid measurement, rc=%d\n", rc);
  2337. bif_ctrl_unlock(ctrl);
  2338. return rc;
  2339. }
  2340. EXPORT_SYMBOL(bif_ctrl_measure_rid);
  2341. /**
  2342. * bif_ctrl_get_bus_period() - get the BIF bus period (tau_bif) in nanoseconds
  2343. * @ctrl: BIF controller consumer handle
  2344. *
  2345. * Returns the currently configured bus period in nanoseconds if successful or
  2346. * errno if an error occurred.
  2347. */
  2348. int bif_ctrl_get_bus_period(struct bif_ctrl *ctrl)
  2349. {
  2350. int rc;
  2351. if (IS_ERR_OR_NULL(ctrl)) {
  2352. pr_err("Invalid controller handle.\n");
  2353. return -ENODEV;
  2354. }
  2355. if (!ctrl->bdev->desc->ops->get_bus_period) {
  2356. pr_err("Cannot get the BIF bus period.\n");
  2357. return -ENXIO;
  2358. }
  2359. rc = ctrl->bdev->desc->ops->get_bus_period(ctrl->bdev);
  2360. if (rc < 0)
  2361. pr_err("Error during bus period retrieval, rc=%d\n", rc);
  2362. return rc;
  2363. }
  2364. EXPORT_SYMBOL(bif_ctrl_get_bus_period);
  2365. /**
  2366. * bif_ctrl_set_bus_period() - set the BIF bus period (tau_bif) in nanoseconds
  2367. * @ctrl: BIF controller consumer handle
  2368. * @period_ns: BIF bus period in nanoseconds to use
  2369. *
  2370. * If the exact period is not supported by the BIF controller hardware, then the
  2371. * next larger supported period will be used.
  2372. *
  2373. * Returns 0 on success or errno if an error occurred.
  2374. */
  2375. int bif_ctrl_set_bus_period(struct bif_ctrl *ctrl, int period_ns)
  2376. {
  2377. int rc;
  2378. if (IS_ERR_OR_NULL(ctrl)) {
  2379. pr_err("Invalid controller handle.\n");
  2380. return -ENODEV;
  2381. }
  2382. if (!ctrl->bdev->desc->ops->set_bus_period) {
  2383. pr_err("Cannot set the BIF bus period.\n");
  2384. return -ENXIO;
  2385. }
  2386. bif_ctrl_lock(ctrl);
  2387. rc = ctrl->bdev->desc->ops->set_bus_period(ctrl->bdev, period_ns);
  2388. if (rc)
  2389. pr_err("Error during bus period configuration, rc=%d\n", rc);
  2390. bif_ctrl_unlock(ctrl);
  2391. return rc;
  2392. }
  2393. EXPORT_SYMBOL(bif_ctrl_set_bus_period);
  2394. /**
  2395. * bif_ctrl_get_bus_state() - get the current state of the BIF bus
  2396. * @ctrl: BIF controller consumer handle
  2397. *
  2398. * Returns a bus state from enum bif_bus_state if successful or errno if an
  2399. * error occurred.
  2400. */
  2401. int bif_ctrl_get_bus_state(struct bif_ctrl *ctrl)
  2402. {
  2403. int rc;
  2404. if (IS_ERR_OR_NULL(ctrl)) {
  2405. pr_err("Invalid controller handle.\n");
  2406. return -ENODEV;
  2407. }
  2408. rc = ctrl->bdev->desc->ops->get_bus_state(ctrl->bdev);
  2409. if (rc < 0)
  2410. pr_err("Error during bus state retrieval, rc=%d\n", rc);
  2411. return rc;
  2412. }
  2413. EXPORT_SYMBOL(bif_ctrl_get_bus_state);
  2414. /**
  2415. * bif_ctrl_set_bus_state() - set the state of the BIF bus
  2416. * @ctrl: BIF controller consumer handle
  2417. * @state: State for the BIF bus to enter
  2418. *
  2419. * Returns 0 on success or errno if an error occurred.
  2420. */
  2421. int bif_ctrl_set_bus_state(struct bif_ctrl *ctrl, enum bif_bus_state state)
  2422. {
  2423. int rc;
  2424. if (IS_ERR_OR_NULL(ctrl)) {
  2425. pr_err("Invalid controller handle.\n");
  2426. return -ENODEV;
  2427. }
  2428. bif_ctrl_lock(ctrl);
  2429. rc = ctrl->bdev->desc->ops->set_bus_state(ctrl->bdev, state);
  2430. if (rc < 0)
  2431. pr_err("Error during bus state configuration, rc=%d\n", rc);
  2432. /*
  2433. * Uncache the selected slave if the new bus state results in the slave
  2434. * becoming unselected.
  2435. */
  2436. if (state == BIF_BUS_STATE_MASTER_DISABLED
  2437. || state == BIF_BUS_STATE_POWER_DOWN
  2438. || state == BIF_BUS_STATE_STANDBY)
  2439. ctrl->bdev->selected_sdev = NULL;
  2440. bif_ctrl_unlock(ctrl);
  2441. return rc;
  2442. }
  2443. EXPORT_SYMBOL(bif_ctrl_set_bus_state);
  2444. /*
  2445. * Check if the specified function is a protocol function and if it is, then
  2446. * instantiate protocol function data for the slave.
  2447. */
  2448. static int bif_initialize_protocol_function(struct bif_slave_dev *sdev,
  2449. struct bif_ddb_l2_data *func)
  2450. {
  2451. int rc = 0;
  2452. u8 buf[4];
  2453. /* Ensure that this is a protocol function. */
  2454. if (func->function_type != BIF_FUNC_PROTOCOL)
  2455. return 0;
  2456. if (sdev->protocol_function) {
  2457. pr_err("Duplicate protocol function found for BIF slave; DEV_ADR=0x%02X\n",
  2458. sdev->slave_addr);
  2459. return -EPERM;
  2460. }
  2461. sdev->protocol_function = kzalloc(sizeof(struct bif_protocol_function),
  2462. GFP_KERNEL);
  2463. if (!sdev->protocol_function) {
  2464. pr_err("out of memory\n");
  2465. return -ENOMEM;
  2466. }
  2467. rc = _bif_slave_read(sdev, func->function_pointer, buf, 4);
  2468. if (rc) {
  2469. pr_err("Protocol function data read failed, rc=%d\n", rc);
  2470. return rc;
  2471. }
  2472. sdev->protocol_function->protocol_pointer = buf[0] << 8 | buf[1];
  2473. sdev->protocol_function->device_id_pointer = buf[2] << 8 | buf[3];
  2474. sdev->protocol_function->l2_entry = func;
  2475. rc = _bif_slave_read(sdev, sdev->protocol_function->device_id_pointer,
  2476. sdev->protocol_function->device_id, BIF_DEVICE_ID_BYTE_LENGTH);
  2477. if (rc) {
  2478. pr_err("Device ID read failed, rc=%d\n", rc);
  2479. return rc;
  2480. }
  2481. /* Check if this slave does not have a UID value stored. */
  2482. if (sdev->unique_id_bits_known == 0) {
  2483. sdev->unique_id_bits_known = BIF_UNIQUE_ID_BIT_LENGTH;
  2484. /* Fill in UID using manufacturer ID and device ID. */
  2485. sdev->unique_id[0] = sdev->l1_data.manufacturer_id >> 8;
  2486. sdev->unique_id[1] = sdev->l1_data.manufacturer_id;
  2487. memcpy(&sdev->unique_id[2],
  2488. sdev->protocol_function->device_id,
  2489. BIF_DEVICE_ID_BYTE_LENGTH);
  2490. }
  2491. return rc;
  2492. }
  2493. /*
  2494. * Check if the specified function is a slave control function and if it is,
  2495. * then instantiate slave control function data for the slave.
  2496. */
  2497. static int bif_initialize_slave_control_function(struct bif_slave_dev *sdev,
  2498. struct bif_ddb_l2_data *func)
  2499. {
  2500. int rc = 0;
  2501. int i;
  2502. u8 buf[3];
  2503. /* Ensure that this is a slave control function. */
  2504. if (func->function_type != BIF_FUNC_SLAVE_CONTROL)
  2505. return 0;
  2506. if (sdev->slave_ctrl_function) {
  2507. pr_err("Duplicate slave control function found for BIF slave; DEV_ADR=0x%02X\n",
  2508. sdev->slave_addr);
  2509. return -EPERM;
  2510. }
  2511. sdev->slave_ctrl_function
  2512. = kzalloc(sizeof(struct bif_protocol_function), GFP_KERNEL);
  2513. if (!sdev->slave_ctrl_function) {
  2514. pr_err("out of memory\n");
  2515. return -ENOMEM;
  2516. }
  2517. rc = _bif_slave_read(sdev, func->function_pointer, buf, 3);
  2518. if (rc) {
  2519. pr_err("Slave control function data read failed, rc=%d\n", rc);
  2520. return rc;
  2521. }
  2522. sdev->slave_ctrl_function->slave_ctrl_pointer = buf[0] << 8 | buf[1];
  2523. sdev->slave_ctrl_function->task_count
  2524. = buf[2] * SLAVE_CTRL_TASKS_PER_SET;
  2525. sdev->slave_ctrl_function->l2_entry = func;
  2526. if (sdev->slave_ctrl_function->task_count > 0) {
  2527. sdev->slave_ctrl_function->irq_notifier_list =
  2528. kzalloc(sizeof(struct blocking_notifier_head)
  2529. * sdev->slave_ctrl_function->task_count,
  2530. GFP_KERNEL);
  2531. if (!sdev->slave_ctrl_function->irq_notifier_list) {
  2532. pr_err("out of memory\n");
  2533. kfree(sdev->slave_ctrl_function);
  2534. return -ENOMEM;
  2535. }
  2536. for (i = 0; i < sdev->slave_ctrl_function->task_count; i++) {
  2537. BLOCKING_INIT_NOTIFIER_HEAD(
  2538. &sdev->slave_ctrl_function->irq_notifier_list[i]);
  2539. }
  2540. }
  2541. return rc;
  2542. }
  2543. /*
  2544. * Check if the specified function is an NVM function and if it is, then
  2545. * instantiate NVM function data for the slave and read all objects.
  2546. */
  2547. static int bif_initialize_nvm_function(struct bif_slave_dev *sdev,
  2548. struct bif_ddb_l2_data *func)
  2549. {
  2550. int rc = 0;
  2551. int data_len, read_size;
  2552. u8 buf[8], object_type;
  2553. struct bif_object *object;
  2554. struct bif_object *temp;
  2555. u16 addr;
  2556. u16 crc;
  2557. /* Ensure that this is an NVM function. */
  2558. if (func->function_type != BIF_FUNC_NVM)
  2559. return 0;
  2560. if (sdev->nvm_function) {
  2561. pr_err("Duplicate NVM function found for BIF slave; DEV_ADR=0x%02X\n",
  2562. sdev->slave_addr);
  2563. return -EPERM;
  2564. }
  2565. sdev->nvm_function
  2566. = kzalloc(sizeof(*sdev->nvm_function), GFP_KERNEL);
  2567. if (!sdev->nvm_function) {
  2568. pr_err("out of memory\n");
  2569. return -ENOMEM;
  2570. }
  2571. rc = _bif_slave_read(sdev, func->function_pointer, buf, 8);
  2572. if (rc) {
  2573. pr_err("NVM function data read failed, rc=%d\n", rc);
  2574. return rc;
  2575. }
  2576. sdev->nvm_function->nvm_pointer = buf[0] << 8 | buf[1];
  2577. sdev->nvm_function->slave_control_channel = buf[2];
  2578. sdev->nvm_function->write_buffer_size = buf[3];
  2579. sdev->nvm_function->nvm_base_address = buf[4] << 8 | buf[5];
  2580. sdev->nvm_function->nvm_size = buf[6] << 8 | buf[7];
  2581. /* Read NVM lock offset */
  2582. rc = _bif_slave_read(sdev, sdev->nvm_function->nvm_pointer, buf, 2);
  2583. if (rc) {
  2584. pr_err("Slave memory read failed, rc=%d\n", rc);
  2585. return rc;
  2586. }
  2587. sdev->nvm_function->nvm_lock_offset = buf[0] << 8 | buf[1];
  2588. INIT_LIST_HEAD(&sdev->nvm_function->object_list);
  2589. /* Read object list */
  2590. addr = sdev->nvm_function->nvm_base_address;
  2591. rc = _bif_slave_read(sdev, addr, &object_type, 1);
  2592. if (rc) {
  2593. pr_err("Slave memory read failed, rc=%d\n", rc);
  2594. return rc;
  2595. }
  2596. while (object_type != BIF_OBJ_END_OF_LIST) {
  2597. object = kzalloc(sizeof(*object), GFP_KERNEL);
  2598. if (!object) {
  2599. pr_err("out of memory\n");
  2600. rc = -ENOMEM;
  2601. goto free_data;
  2602. }
  2603. list_add_tail(&object->list, &sdev->nvm_function->object_list);
  2604. rc = _bif_slave_read(sdev, addr + 1, buf + 1, 5);
  2605. if (rc) {
  2606. pr_err("Slave memory read of object header failed; addr=0x%04X, len=%d, rc=%d\n",
  2607. addr + 1, 5, rc);
  2608. goto free_data;
  2609. }
  2610. object->addr = addr;
  2611. object->type = object_type;
  2612. object->version = buf[1];
  2613. object->manufacturer_id = buf[2] << 8 | buf[3];
  2614. object->length = buf[4] << 8 | buf[5];
  2615. if ((object->addr + object->length)
  2616. > (sdev->nvm_function->nvm_base_address
  2617. + sdev->nvm_function->nvm_size)) {
  2618. pr_warn("warning: BIF slave object is not formatted correctly; NVM base=0x%04X, NVM len=%d, object addr=0x%04X, object len=%d\n",
  2619. sdev->nvm_function->nvm_base_address,
  2620. sdev->nvm_function->nvm_size,
  2621. object->addr,
  2622. object->length);
  2623. /* Limit object size to remaining NVM size. */
  2624. object->length = sdev->nvm_function->nvm_size
  2625. + sdev->nvm_function->nvm_base_address
  2626. - object->addr;
  2627. }
  2628. /* Object header + CRC takes up 8 bytes. */
  2629. data_len = object->length - 8;
  2630. object->data = kmalloc(data_len, GFP_KERNEL);
  2631. if (!object->data) {
  2632. pr_err("out of memory\n");
  2633. rc = -ENOMEM;
  2634. goto free_data;
  2635. }
  2636. rc = _bif_slave_read(sdev, addr + 6, object->data, data_len);
  2637. if (rc) {
  2638. pr_err("Slave memory read of object data failed; addr=0x%04X, len=%d, rc=%d\n",
  2639. addr + 6, data_len, rc);
  2640. goto free_data;
  2641. }
  2642. if ((object->length + addr) >= (sdev->nvm_function->nvm_size
  2643. + sdev->nvm_function->nvm_base_address))
  2644. read_size = 2;
  2645. else
  2646. read_size = 3;
  2647. rc = _bif_slave_read(sdev, addr + 6 + data_len, buf, read_size);
  2648. if (rc) {
  2649. pr_err("Slave memory read of object CRC failed; addr=0x%04X, len=%d, rc=%d\n",
  2650. addr + 6 + data_len, read_size, rc);
  2651. goto free_data;
  2652. }
  2653. object->crc = buf[0] << 8 | buf[1];
  2654. object_type = (read_size == 3) ? buf[2] : BIF_OBJ_END_OF_LIST;
  2655. sdev->nvm_function->object_count++;
  2656. crc = bif_object_crc_ccitt(object);
  2657. if (crc != object->crc)
  2658. pr_info("BIF object at addr=0x%04X has invalid CRC; crc calc=0x%04X, crc exp=0x%04X\n",
  2659. object->addr, crc, object->crc);
  2660. addr += object->length;
  2661. }
  2662. return rc;
  2663. free_data:
  2664. list_for_each_entry_safe(object, temp,
  2665. &sdev->nvm_function->object_list, list) {
  2666. list_del(&object->list);
  2667. kfree(object->data);
  2668. kfree(object);
  2669. }
  2670. kfree(sdev->nvm_function);
  2671. sdev->nvm_function = NULL;
  2672. return rc;
  2673. }
  2674. static int bif_parse_slave_data(struct bif_slave_dev *sdev)
  2675. {
  2676. int rc = 0;
  2677. u8 buf[10];
  2678. u8 *func_buf;
  2679. struct bif_ddb_l2_data *func;
  2680. int function_count, i;
  2681. rc = _bif_slave_read(sdev, BIF_DDB_L1_BASE_ADDR, buf, 10);
  2682. if (rc) {
  2683. pr_err("DDB L1 data read failed, rc=%d\n", rc);
  2684. return rc;
  2685. }
  2686. sdev->l1_data.revision = buf[0];
  2687. sdev->l1_data.level = buf[1];
  2688. sdev->l1_data.device_class = buf[2] << 8 | buf[3];
  2689. sdev->l1_data.manufacturer_id = buf[4] << 8 | buf[5];
  2690. sdev->l1_data.product_id = buf[6] << 8 | buf[7];
  2691. sdev->l1_data.length = buf[8] << 8 | buf[9];
  2692. function_count = sdev->l1_data.length / 4;
  2693. if (sdev->l1_data.length % 4) {
  2694. pr_err("Function directory length=%d is invalid\n",
  2695. sdev->l1_data.length);
  2696. return -EPROTO;
  2697. }
  2698. /* No DDB L2 function directory */
  2699. if (function_count == 0)
  2700. return 0;
  2701. func_buf = kmalloc(sdev->l1_data.length, GFP_KERNEL);
  2702. if (!func_buf) {
  2703. pr_err("out of memory\n");
  2704. return -ENOMEM;
  2705. }
  2706. sdev->function_directory = kzalloc(
  2707. function_count * sizeof(struct bif_ddb_l2_data), GFP_KERNEL);
  2708. if (!sdev->function_directory) {
  2709. pr_err("out of memory\n");
  2710. return -ENOMEM;
  2711. }
  2712. rc = _bif_slave_read(sdev, BIF_DDB_L2_BASE_ADDR, func_buf,
  2713. sdev->l1_data.length);
  2714. if (rc) {
  2715. pr_err("DDB L2 data read failed, rc=%d\n", rc);
  2716. return rc;
  2717. }
  2718. for (i = 0; i < function_count; i++) {
  2719. func = &sdev->function_directory[i];
  2720. func->function_type = func_buf[i * 4];
  2721. func->function_version = func_buf[i * 4 + 1];
  2722. func->function_pointer = func_buf[i * 4 + 2] << 8
  2723. | func_buf[i * 4 + 3];
  2724. rc = bif_initialize_protocol_function(sdev, func);
  2725. if (rc)
  2726. goto done;
  2727. rc = bif_initialize_slave_control_function(sdev, func);
  2728. if (rc)
  2729. goto done;
  2730. rc = bif_initialize_nvm_function(sdev, func);
  2731. if (rc)
  2732. goto done;
  2733. }
  2734. done:
  2735. kfree(func_buf);
  2736. return rc;
  2737. }
  2738. static int bif_add_secondary_slaves(struct bif_slave_dev *primary_slave)
  2739. {
  2740. int rc = 0;
  2741. int data_len, i;
  2742. u16 crc;
  2743. struct bif_slave_dev *sdev;
  2744. struct bif_object *object;
  2745. list_for_each_entry(object, &primary_slave->nvm_function->object_list,
  2746. list) {
  2747. if (object->type != BIF_OBJ_SEC_SLAVE)
  2748. continue;
  2749. data_len = object->length - 8;
  2750. if (data_len % BIF_UNIQUE_ID_BYTE_LENGTH) {
  2751. pr_info("Invalid secondary slave object found, addr=0x%04X, data len=%d\n",
  2752. object->addr, data_len);
  2753. continue;
  2754. }
  2755. crc = bif_object_crc_ccitt(object);
  2756. if (crc != object->crc) {
  2757. pr_info("BIF object at addr=0x%04X has invalid CRC; crc calc=0x%04X, crc exp=0x%04X\n",
  2758. object->addr, crc, object->crc);
  2759. continue;
  2760. }
  2761. for (i = 0; i < data_len / BIF_UNIQUE_ID_BYTE_LENGTH; i++) {
  2762. sdev = bif_add_slave(primary_slave->bdev);
  2763. if (IS_ERR(sdev)) {
  2764. rc = PTR_ERR(sdev);
  2765. pr_err("bif_add_slave failed, rc=%d\n", rc);
  2766. return rc;
  2767. }
  2768. memcpy(sdev->unique_id,
  2769. &object->data[i * BIF_UNIQUE_ID_BYTE_LENGTH],
  2770. BIF_UNIQUE_ID_BYTE_LENGTH);
  2771. sdev->unique_id_bits_known = BIF_UNIQUE_ID_BIT_LENGTH;
  2772. rc = bif_select_slave(sdev);
  2773. if (rc) {
  2774. pr_err("Could not select slave, rc=%d\n", rc);
  2775. goto free_slave;
  2776. }
  2777. rc = bif_is_slave_selected(sdev->bdev);
  2778. if (rc < 0) {
  2779. pr_err("Transaction failed, rc=%d\n", rc);
  2780. goto free_slave;
  2781. } else if (rc == 1) {
  2782. sdev->present = true;
  2783. sdev->bdev->selected_sdev = sdev;
  2784. rc = bif_parse_slave_data(sdev);
  2785. if (rc) {
  2786. pr_err("Failed to parse secondary slave data, rc=%d\n",
  2787. rc);
  2788. goto free_slave;
  2789. }
  2790. } else {
  2791. sdev->present = false;
  2792. sdev->bdev->selected_sdev = NULL;
  2793. }
  2794. }
  2795. }
  2796. return rc;
  2797. free_slave:
  2798. bif_remove_slave(sdev);
  2799. return rc;
  2800. }
  2801. /*
  2802. * Performs UID search to identify all slaves attached to the bus. Assumes that
  2803. * all necessary locks are held.
  2804. */
  2805. static int bif_perform_uid_search(struct bif_ctrl_dev *bdev)
  2806. {
  2807. struct bif_slave_dev *sdev;
  2808. struct bif_slave_dev *new_slave;
  2809. bool resp[2], resp_dilc;
  2810. int i;
  2811. int rc = 0;
  2812. u8 cmd_probe[2] = {BIF_CMD_DIP0, BIF_CMD_DIP1};
  2813. u8 cmd_enter[2] = {BIF_CMD_DIE0, BIF_CMD_DIE1};
  2814. /*
  2815. * Iterate over all partially known UIDs adding new ones as they are
  2816. * found.
  2817. */
  2818. list_for_each_entry(sdev, &bif_sdev_list, list) {
  2819. /* Skip slaves with fully known UIDs. */
  2820. if (sdev->unique_id_bits_known == BIF_UNIQUE_ID_BIT_LENGTH
  2821. || sdev->bdev != bdev)
  2822. continue;
  2823. /* Begin a new UID search. */
  2824. rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_BC,
  2825. BIF_CMD_DISS);
  2826. if (rc) {
  2827. pr_err("bus_transaction failed, rc=%d\n", rc);
  2828. return rc;
  2829. }
  2830. /* Step through all known UID bits (MSB to LSB). */
  2831. for (i = 0; i < sdev->unique_id_bits_known; i++) {
  2832. rc = bdev->desc->ops->bus_transaction(bdev,
  2833. BIF_TRANS_BC,
  2834. cmd_enter[get_uid_bit(sdev->unique_id, i)]);
  2835. if (rc) {
  2836. pr_err("bus_transaction failed, rc=%d\n", rc);
  2837. return rc;
  2838. }
  2839. }
  2840. /* Step through unknown UID bits. */
  2841. for (i = sdev->unique_id_bits_known;
  2842. i < BIF_UNIQUE_ID_BIT_LENGTH; i++) {
  2843. rc = bdev->desc->ops->bus_transaction_query(bdev,
  2844. BIF_TRANS_BC, cmd_probe[0], &resp[0]);
  2845. if (rc) {
  2846. pr_err("bus_transaction failed, rc=%d\n", rc);
  2847. return rc;
  2848. }
  2849. rc = bdev->desc->ops->bus_transaction_query(bdev,
  2850. BIF_TRANS_BC, cmd_probe[1], &resp[1]);
  2851. if (rc) {
  2852. pr_err("bus_transaction failed, rc=%d\n", rc);
  2853. return rc;
  2854. }
  2855. if (resp[0] && resp[1]) {
  2856. /* Create an entry for the new UID branch. */
  2857. new_slave = bif_add_slave(bdev);
  2858. if (IS_ERR(new_slave)) {
  2859. rc = PTR_ERR(sdev);
  2860. pr_err("bif_add_slave failed, rc=%d\n",
  2861. rc);
  2862. return rc;
  2863. }
  2864. memcpy(new_slave->unique_id, sdev->unique_id,
  2865. BIF_UNIQUE_ID_BYTE_LENGTH);
  2866. new_slave->bdev = sdev->bdev;
  2867. set_uid_bit(sdev->unique_id, i, 0);
  2868. sdev->unique_id_bits_known = i + 1;
  2869. set_uid_bit(new_slave->unique_id, i, 1);
  2870. new_slave->unique_id_bits_known = i + 1;
  2871. } else if (resp[0]) {
  2872. set_uid_bit(sdev->unique_id, i, 0);
  2873. sdev->unique_id_bits_known = i + 1;
  2874. } else if (resp[1]) {
  2875. set_uid_bit(sdev->unique_id, i, 1);
  2876. sdev->unique_id_bits_known = i + 1;
  2877. } else {
  2878. pr_debug("no bus query response received\n");
  2879. rc = -ENXIO;
  2880. return rc;
  2881. }
  2882. rc = bdev->desc->ops->bus_transaction(bdev,
  2883. BIF_TRANS_BC, cmd_enter[resp[0] ? 0 : 1]);
  2884. if (rc) {
  2885. pr_err("bus_transaction failed, rc=%d\n", rc);
  2886. return rc;
  2887. }
  2888. }
  2889. rc = bdev->desc->ops->bus_transaction_query(bdev,
  2890. BIF_TRANS_BC, BIF_CMD_DILC, &resp_dilc);
  2891. if (rc) {
  2892. pr_err("bus_transaction failed, rc=%d\n", rc);
  2893. return rc;
  2894. }
  2895. if (resp_dilc) {
  2896. sdev->present = true;
  2897. sdev->bdev->selected_sdev = sdev;
  2898. rc = bif_parse_slave_data(sdev);
  2899. if (rc) {
  2900. pr_err("Failed to parse secondary slave data, rc=%d\n",
  2901. rc);
  2902. return rc;
  2903. }
  2904. } else {
  2905. pr_err("Slave failed to respond to DILC bus command; its UID is thus unverified.\n");
  2906. sdev->unique_id_bits_known = 0;
  2907. rc = -ENXIO;
  2908. return rc;
  2909. }
  2910. }
  2911. return rc;
  2912. }
  2913. /*
  2914. * Removes slaves from the bif_sdev_list which have the same UID as previous
  2915. * slaves in the list.
  2916. */
  2917. static int bif_remove_duplicate_slaves(struct bif_ctrl_dev *bdev)
  2918. {
  2919. struct bif_slave_dev *sdev;
  2920. struct bif_slave_dev *last_slave;
  2921. struct bif_slave_dev *temp;
  2922. list_for_each_entry_safe(last_slave, temp, &bif_sdev_list, list) {
  2923. list_for_each_entry(sdev, &bif_sdev_list, list) {
  2924. if (last_slave == sdev) {
  2925. break;
  2926. } else if (memcmp(last_slave->unique_id,
  2927. sdev->unique_id,
  2928. BIF_UNIQUE_ID_BYTE_LENGTH) == 0) {
  2929. bif_remove_slave(last_slave);
  2930. break;
  2931. }
  2932. }
  2933. }
  2934. return 0;
  2935. }
  2936. static int bif_add_all_slaves(struct bif_ctrl_dev *bdev)
  2937. {
  2938. struct bif_slave_dev *sdev;
  2939. int rc = 0;
  2940. int i;
  2941. bool has_slave = false, is_primary_slave = false;
  2942. mutex_lock(&bif_sdev_list_mutex);
  2943. mutex_lock(&bdev->mutex);
  2944. list_for_each_entry(sdev, &bif_sdev_list, list) {
  2945. if (sdev->bdev == bdev) {
  2946. has_slave = true;
  2947. break;
  2948. }
  2949. }
  2950. if (!has_slave) {
  2951. /* Create a single empty slave to start the search algorithm. */
  2952. sdev = bif_add_slave(bdev);
  2953. if (IS_ERR(sdev)) {
  2954. rc = PTR_ERR(sdev);
  2955. pr_err("bif_add_slave failed, rc=%d\n", rc);
  2956. goto out;
  2957. }
  2958. for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
  2959. /* Attempt to select primary slave in battery pack. */
  2960. rc = bdev->desc->ops->bus_transaction(bdev,
  2961. BIF_TRANS_SDA, BIF_PRIMARY_SLAVE_DEV_ADR);
  2962. if (rc == 0)
  2963. break;
  2964. }
  2965. if (rc) {
  2966. pr_err("BIF bus_transaction failed, rc=%d\n", rc);
  2967. goto out;
  2968. }
  2969. /* Check if a slave is selected. */
  2970. rc = bif_is_slave_selected(bdev);
  2971. if (rc < 0) {
  2972. pr_err("BIF bus_transaction failed, rc=%d\n", rc);
  2973. goto out;
  2974. } else {
  2975. is_primary_slave = rc;
  2976. }
  2977. }
  2978. if (is_primary_slave) {
  2979. pr_debug("Using primary slave at DEV_ADR==0x%02X\n",
  2980. BIF_PRIMARY_SLAVE_DEV_ADR);
  2981. sdev->bdev->selected_sdev = sdev;
  2982. sdev->present = true;
  2983. sdev->slave_addr = BIF_PRIMARY_SLAVE_DEV_ADR;
  2984. rc = bif_parse_slave_data(sdev);
  2985. if (rc) {
  2986. pr_err("Failed to parse primary slave data, rc=%d\n",
  2987. rc);
  2988. goto out;
  2989. }
  2990. rc = bif_add_secondary_slaves(sdev);
  2991. if (rc) {
  2992. pr_err("Failed to add secondary slaves, rc=%d\n", rc);
  2993. goto out;
  2994. }
  2995. } else {
  2996. pr_debug("Falling back on full UID search.\n");
  2997. for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
  2998. rc = bif_perform_uid_search(bdev);
  2999. if (rc == 0)
  3000. break;
  3001. }
  3002. if (rc) {
  3003. pr_debug("BIF UID search failed, rc=%d\n", rc);
  3004. goto out;
  3005. }
  3006. }
  3007. bif_remove_duplicate_slaves(bdev);
  3008. mutex_unlock(&bdev->mutex);
  3009. mutex_unlock(&bif_sdev_list_mutex);
  3010. return rc;
  3011. out:
  3012. mutex_unlock(&bdev->mutex);
  3013. mutex_unlock(&bif_sdev_list_mutex);
  3014. pr_debug("BIF slave search failed, rc=%d\n", rc);
  3015. return rc;
  3016. }
  3017. static int bif_add_known_slave(struct bif_ctrl_dev *bdev, u8 slave_addr)
  3018. {
  3019. struct bif_slave_dev *sdev;
  3020. int rc = 0;
  3021. int i;
  3022. for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
  3023. /* Attempt to select the slave. */
  3024. rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_SDA,
  3025. slave_addr);
  3026. if (rc == 0)
  3027. break;
  3028. }
  3029. if (rc) {
  3030. pr_err("BIF bus_transaction failed, rc=%d\n", rc);
  3031. return rc;
  3032. }
  3033. /* Check if a slave is selected. */
  3034. rc = bif_is_slave_selected(bdev);
  3035. if (rc < 0) {
  3036. pr_err("BIF bus_transaction failed, rc=%d\n", rc);
  3037. return rc;
  3038. }
  3039. sdev = bif_add_slave(bdev);
  3040. if (IS_ERR(sdev)) {
  3041. rc = PTR_ERR(sdev);
  3042. pr_err("bif_add_slave failed, rc=%d\n", rc);
  3043. return rc;
  3044. }
  3045. sdev->bdev->selected_sdev = sdev;
  3046. sdev->present = true;
  3047. sdev->slave_addr = slave_addr;
  3048. rc = bif_parse_slave_data(sdev);
  3049. if (rc) {
  3050. pr_err("Failed to parse slave data, addr=0x%02X, rc=%d\n",
  3051. slave_addr, rc);
  3052. return rc;
  3053. }
  3054. return rc;
  3055. }
  3056. static int bif_add_known_slaves_from_dt(struct bif_ctrl_dev *bdev,
  3057. struct device_node *of_node)
  3058. {
  3059. int len = 0;
  3060. int rc, i;
  3061. u32 addr;
  3062. const __be32 *val;
  3063. mutex_lock(&bif_sdev_list_mutex);
  3064. mutex_lock(&bdev->mutex);
  3065. val = of_get_property(of_node, "qcom,known-device-addresses", &len);
  3066. len /= sizeof(u32);
  3067. if (val && len == 0) {
  3068. pr_err("qcom,known-device-addresses property is invalid\n");
  3069. rc = -EINVAL;
  3070. goto out;
  3071. }
  3072. for (i = 0; i < len; i++) {
  3073. addr = be32_to_cpup(val++);
  3074. if (addr == 0x00 || addr > 0xFF) {
  3075. rc = -EINVAL;
  3076. pr_err("qcom,known-device-addresses property contains invalid address=0x%X\n",
  3077. addr);
  3078. goto out;
  3079. }
  3080. rc = bif_add_known_slave(bdev, addr);
  3081. if (rc) {
  3082. pr_err("bif_add_known_slave() failed, rc=%d\n", rc);
  3083. goto out;
  3084. }
  3085. }
  3086. out:
  3087. if (len > 0)
  3088. bif_remove_duplicate_slaves(bdev);
  3089. mutex_unlock(&bdev->mutex);
  3090. mutex_unlock(&bif_sdev_list_mutex);
  3091. return rc;
  3092. }
  3093. /*
  3094. * Programs a device address for the specified slave in order to simplify
  3095. * slave selection in the future.
  3096. */
  3097. static int bif_assign_slave_dev_addr(struct bif_slave_dev *sdev, u8 dev_addr)
  3098. {
  3099. int rc;
  3100. u16 addr;
  3101. if (!sdev->protocol_function) {
  3102. pr_err("Protocol function not present; cannot set device address.\n");
  3103. return -ENODEV;
  3104. }
  3105. addr = PROTOCOL_FUNC_DEV_ADR_ADDR(
  3106. sdev->protocol_function->protocol_pointer);
  3107. rc = _bif_slave_write(sdev, addr, &dev_addr, 1);
  3108. if (rc)
  3109. pr_err("Failed to set slave device address.\n");
  3110. else
  3111. sdev->slave_addr = dev_addr;
  3112. return rc;
  3113. }
  3114. /* Assigns a unique device address to all slaves which do not have one. */
  3115. static int bif_assign_all_slaves_dev_addr(struct bif_ctrl_dev *bdev)
  3116. {
  3117. struct bif_slave_dev *sdev;
  3118. struct bif_slave_dev *sibling;
  3119. bool duplicate;
  3120. int rc = 0;
  3121. u8 dev_addr, first_dev_addr;
  3122. mutex_lock(&bif_sdev_list_mutex);
  3123. mutex_lock(&bdev->mutex);
  3124. first_dev_addr = next_dev_addr;
  3125. /*
  3126. * Iterate over all partially known UIDs adding new ones as they are
  3127. * found.
  3128. */
  3129. list_for_each_entry(sdev, &bif_sdev_list, list) {
  3130. /*
  3131. * Skip slaves without known UIDs, which already have a device
  3132. * address or which aren't present.
  3133. */
  3134. if (sdev->unique_id_bits_known != BIF_UNIQUE_ID_BIT_LENGTH
  3135. || sdev->slave_addr != 0x00 || !sdev->present)
  3136. continue;
  3137. do {
  3138. dev_addr = next_dev_addr;
  3139. duplicate = false;
  3140. list_for_each_entry(sibling, &bif_sdev_list, list) {
  3141. if (sibling->slave_addr == dev_addr) {
  3142. duplicate = true;
  3143. break;
  3144. }
  3145. }
  3146. next_dev_addr = dev_addr + 1;
  3147. } while (duplicate && (next_dev_addr != first_dev_addr));
  3148. if (next_dev_addr == first_dev_addr) {
  3149. pr_err("No more BIF slave device addresses available.\n");
  3150. rc = -ENODEV;
  3151. goto out;
  3152. }
  3153. rc = bif_assign_slave_dev_addr(sdev, dev_addr);
  3154. if (rc) {
  3155. pr_err("Failed to set slave address.\n");
  3156. goto out;
  3157. }
  3158. }
  3159. mutex_unlock(&bdev->mutex);
  3160. mutex_unlock(&bif_sdev_list_mutex);
  3161. return rc;
  3162. out:
  3163. mutex_unlock(&bdev->mutex);
  3164. mutex_unlock(&bif_sdev_list_mutex);
  3165. pr_err("BIF slave device address setting failed, rc=%d\n", rc);
  3166. return rc;
  3167. }
  3168. /**
  3169. * bdev_get_drvdata() - get the private BIF controller driver data
  3170. * @bdev: BIF controller device pointer
  3171. */
  3172. void *bdev_get_drvdata(struct bif_ctrl_dev *bdev)
  3173. {
  3174. return bdev->driver_data;
  3175. }
  3176. EXPORT_SYMBOL(bdev_get_drvdata);
  3177. static const char * const battery_label[] = {
  3178. "unknown",
  3179. "none",
  3180. "special 1",
  3181. "special 2",
  3182. "special 3",
  3183. "low cost",
  3184. "smart",
  3185. };
  3186. static const char *bif_get_battery_pack_type(int rid_ohm)
  3187. {
  3188. const char *label = battery_label[0];
  3189. if (rid_ohm > BIF_BATT_RID_SMART_MAX)
  3190. label = battery_label[1];
  3191. else if (rid_ohm >= BIF_BATT_RID_SMART_MIN)
  3192. label = battery_label[6];
  3193. else if (rid_ohm >= BIF_BATT_RID_LOW_COST_MIN
  3194. && rid_ohm <= BIF_BATT_RID_LOW_COST_MAX)
  3195. label = battery_label[5];
  3196. else if (rid_ohm >= BIF_BATT_RID_SPECIAL3_MIN
  3197. && rid_ohm <= BIF_BATT_RID_SPECIAL3_MAX)
  3198. label = battery_label[4];
  3199. else if (rid_ohm >= BIF_BATT_RID_SPECIAL2_MIN
  3200. && rid_ohm <= BIF_BATT_RID_SPECIAL2_MAX)
  3201. label = battery_label[3];
  3202. else if (rid_ohm >= BIF_BATT_RID_SPECIAL1_MIN
  3203. && rid_ohm <= BIF_BATT_RID_SPECIAL1_MAX)
  3204. label = battery_label[2];
  3205. return label;
  3206. }
  3207. /**
  3208. * bif_ctrl_register() - register a BIF controller with the BIF framework
  3209. * @bif_desc: Pointer to BIF controller descriptor
  3210. * @dev: Device pointer of the BIF controller
  3211. * @driver_data: Private driver data to associate with the BIF controller
  3212. * @of_node Pointer to the device tree node of the BIF controller
  3213. *
  3214. * Returns a BIF controller device pointer for the controller if registration
  3215. * is successful or an ERR_PTR if an error occurred.
  3216. */
  3217. struct bif_ctrl_dev *bif_ctrl_register(struct bif_ctrl_desc *bif_desc,
  3218. struct device *dev, void *driver_data, struct device_node *of_node)
  3219. {
  3220. struct bif_ctrl_dev *bdev = ERR_PTR(-EINVAL);
  3221. struct bif_slave_dev *sdev;
  3222. bool battery_present = false;
  3223. bool slaves_present = false;
  3224. int rc, rid_ohm;
  3225. if (!bif_desc) {
  3226. pr_err("Invalid bif_desc specified\n");
  3227. return bdev;
  3228. } else if (!bif_desc->name) {
  3229. pr_err("BIF name missing\n");
  3230. return bdev;
  3231. } else if (!bif_desc->ops) {
  3232. pr_err("BIF operations missing\n");
  3233. return bdev;
  3234. } else if (!bif_desc->ops->bus_transaction
  3235. || !bif_desc->ops->bus_transaction_query
  3236. || !bif_desc->ops->bus_transaction_read
  3237. || !bif_desc->ops->get_bus_state
  3238. || !bif_desc->ops->set_bus_state) {
  3239. pr_err("BIF operation callback function(s) missing\n");
  3240. return bdev;
  3241. }
  3242. bdev = kzalloc(sizeof(struct bif_ctrl_dev), GFP_KERNEL);
  3243. if (bdev == NULL) {
  3244. pr_err("Memory allocation failed for bif_ctrl_dev\n");
  3245. return ERR_PTR(-ENOMEM);
  3246. }
  3247. mutex_init(&bdev->mutex);
  3248. INIT_LIST_HEAD(&bdev->list);
  3249. INIT_DELAYED_WORK(&bdev->enter_irq_mode_work, bif_enter_irq_mode_work);
  3250. bdev->desc = bif_desc;
  3251. bdev->ctrl_dev = dev;
  3252. bdev->driver_data = driver_data;
  3253. bdev->irq_mode_delay_jiffies = 2;
  3254. mutex_lock(&bif_ctrl_list_mutex);
  3255. list_add_tail(&bdev->list, &bif_ctrl_list);
  3256. mutex_unlock(&bif_ctrl_list_mutex);
  3257. rc = bif_add_all_slaves(bdev);
  3258. if (rc)
  3259. pr_debug("Search for all slaves failed, rc=%d\n", rc);
  3260. rc = bif_add_known_slaves_from_dt(bdev, of_node);
  3261. if (rc)
  3262. pr_err("Adding slaves based on device tree addressed failed, rc=%d.\n",
  3263. rc);
  3264. rc = bif_assign_all_slaves_dev_addr(bdev);
  3265. if (rc)
  3266. pr_err("Failed to set slave device address, rc=%d\n", rc);
  3267. bif_print_slaves();
  3268. if (bdev->desc->ops->get_battery_presence) {
  3269. rc = bdev->desc->ops->get_battery_presence(bdev);
  3270. if (rc < 0) {
  3271. pr_err("Could not determine battery presence, rc=%d\n",
  3272. rc);
  3273. } else {
  3274. battery_present = rc;
  3275. pr_info("Battery pack present = %c\n", rc ? 'Y' : 'N');
  3276. }
  3277. }
  3278. if (bdev->desc->ops->get_battery_rid) {
  3279. rid_ohm = bdev->desc->ops->get_battery_rid(bdev);
  3280. if (rid_ohm >= 0)
  3281. pr_info("Battery pack type = %s (Rid=%d ohm)\n",
  3282. bif_get_battery_pack_type(rid_ohm), rid_ohm);
  3283. else
  3284. pr_err("Could not read Rid, rc=%d\n", rid_ohm);
  3285. }
  3286. list_for_each_entry(sdev, &bif_sdev_list, list) {
  3287. if (sdev->present) {
  3288. battery_present = true;
  3289. slaves_present = true;
  3290. break;
  3291. }
  3292. }
  3293. BLOCKING_INIT_NOTIFIER_HEAD(&bdev->bus_change_notifier);
  3294. /* Disable the BIF bus master if no slaves are found. */
  3295. if (!slaves_present) {
  3296. rc = bdev->desc->ops->set_bus_state(bdev,
  3297. BIF_BUS_STATE_MASTER_DISABLED);
  3298. if (rc < 0)
  3299. pr_err("Could not disble BIF master, rc=%d\n", rc);
  3300. }
  3301. if (battery_present) {
  3302. bdev->battery_present = true;
  3303. rc = blocking_notifier_call_chain(&bdev->bus_change_notifier,
  3304. BIF_BUS_EVENT_BATTERY_INSERTED, bdev);
  3305. if (rc)
  3306. pr_err("Call chain noification failed, rc=%d\n", rc);
  3307. }
  3308. return bdev;
  3309. }
  3310. EXPORT_SYMBOL(bif_ctrl_register);
  3311. /**
  3312. * bif_ctrl_unregister() - unregisters a BIF controller
  3313. * @bdev: BIF controller device pointer
  3314. */
  3315. void bif_ctrl_unregister(struct bif_ctrl_dev *bdev)
  3316. {
  3317. if (bdev) {
  3318. mutex_lock(&bif_ctrl_list_mutex);
  3319. list_del(&bdev->list);
  3320. mutex_unlock(&bif_ctrl_list_mutex);
  3321. }
  3322. }
  3323. EXPORT_SYMBOL(bif_ctrl_unregister);