extcon.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/extcon/extcon.c - External Connector (extcon) framework.
  4. *
  5. * Copyright (C) 2015 Samsung Electronics
  6. * Author: Chanwoo Choi <cw00.choi@samsung.com>
  7. *
  8. * Copyright (C) 2012 Samsung Electronics
  9. * Author: Donggeun Kim <dg77.kim@samsung.com>
  10. * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
  11. *
  12. * based on android/drivers/switch/switch_class.c
  13. * Copyright (C) 2008 Google, Inc.
  14. * Author: Mike Lockwood <lockwood@android.com>
  15. */
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/init.h>
  19. #include <linux/device.h>
  20. #include <linux/fs.h>
  21. #include <linux/err.h>
  22. #include <linux/of.h>
  23. #include <linux/slab.h>
  24. #include <linux/sysfs.h>
  25. #include "extcon.h"
  26. #define SUPPORTED_CABLE_MAX 32
  27. static const struct __extcon_info {
  28. unsigned int type;
  29. unsigned int id;
  30. const char *name;
  31. } extcon_info[] = {
  32. [EXTCON_NONE] = {
  33. .type = EXTCON_TYPE_MISC,
  34. .id = EXTCON_NONE,
  35. .name = "NONE",
  36. },
  37. /* USB external connector */
  38. [EXTCON_USB] = {
  39. .type = EXTCON_TYPE_USB,
  40. .id = EXTCON_USB,
  41. .name = "USB",
  42. },
  43. [EXTCON_USB_HOST] = {
  44. .type = EXTCON_TYPE_USB,
  45. .id = EXTCON_USB_HOST,
  46. .name = "USB-HOST",
  47. },
  48. /* Charging external connector */
  49. [EXTCON_CHG_USB_SDP] = {
  50. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  51. .id = EXTCON_CHG_USB_SDP,
  52. .name = "SDP",
  53. },
  54. [EXTCON_CHG_USB_DCP] = {
  55. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  56. .id = EXTCON_CHG_USB_DCP,
  57. .name = "DCP",
  58. },
  59. [EXTCON_CHG_USB_CDP] = {
  60. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  61. .id = EXTCON_CHG_USB_CDP,
  62. .name = "CDP",
  63. },
  64. [EXTCON_CHG_USB_ACA] = {
  65. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  66. .id = EXTCON_CHG_USB_ACA,
  67. .name = "ACA",
  68. },
  69. [EXTCON_CHG_USB_FAST] = {
  70. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  71. .id = EXTCON_CHG_USB_FAST,
  72. .name = "FAST-CHARGER",
  73. },
  74. [EXTCON_CHG_USB_SLOW] = {
  75. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  76. .id = EXTCON_CHG_USB_SLOW,
  77. .name = "SLOW-CHARGER",
  78. },
  79. [EXTCON_CHG_WPT] = {
  80. .type = EXTCON_TYPE_CHG,
  81. .id = EXTCON_CHG_WPT,
  82. .name = "WPT",
  83. },
  84. [EXTCON_CHG_USB_PD] = {
  85. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  86. .id = EXTCON_CHG_USB_PD,
  87. .name = "PD",
  88. },
  89. /* Jack external connector */
  90. [EXTCON_JACK_MICROPHONE] = {
  91. .type = EXTCON_TYPE_JACK,
  92. .id = EXTCON_JACK_MICROPHONE,
  93. .name = "MICROPHONE",
  94. },
  95. [EXTCON_JACK_HEADPHONE] = {
  96. .type = EXTCON_TYPE_JACK,
  97. .id = EXTCON_JACK_HEADPHONE,
  98. .name = "HEADPHONE",
  99. },
  100. [EXTCON_JACK_LINE_IN] = {
  101. .type = EXTCON_TYPE_JACK,
  102. .id = EXTCON_JACK_LINE_IN,
  103. .name = "LINE-IN",
  104. },
  105. [EXTCON_JACK_LINE_OUT] = {
  106. .type = EXTCON_TYPE_JACK,
  107. .id = EXTCON_JACK_LINE_OUT,
  108. .name = "LINE-OUT",
  109. },
  110. [EXTCON_JACK_VIDEO_IN] = {
  111. .type = EXTCON_TYPE_JACK,
  112. .id = EXTCON_JACK_VIDEO_IN,
  113. .name = "VIDEO-IN",
  114. },
  115. [EXTCON_JACK_VIDEO_OUT] = {
  116. .type = EXTCON_TYPE_JACK,
  117. .id = EXTCON_JACK_VIDEO_OUT,
  118. .name = "VIDEO-OUT",
  119. },
  120. [EXTCON_JACK_SPDIF_IN] = {
  121. .type = EXTCON_TYPE_JACK,
  122. .id = EXTCON_JACK_SPDIF_IN,
  123. .name = "SPDIF-IN",
  124. },
  125. [EXTCON_JACK_SPDIF_OUT] = {
  126. .type = EXTCON_TYPE_JACK,
  127. .id = EXTCON_JACK_SPDIF_OUT,
  128. .name = "SPDIF-OUT",
  129. },
  130. /* Display external connector */
  131. [EXTCON_DISP_HDMI] = {
  132. .type = EXTCON_TYPE_DISP,
  133. .id = EXTCON_DISP_HDMI,
  134. .name = "HDMI",
  135. },
  136. [EXTCON_DISP_MHL] = {
  137. .type = EXTCON_TYPE_DISP,
  138. .id = EXTCON_DISP_MHL,
  139. .name = "MHL",
  140. },
  141. [EXTCON_DISP_DVI] = {
  142. .type = EXTCON_TYPE_DISP,
  143. .id = EXTCON_DISP_DVI,
  144. .name = "DVI",
  145. },
  146. [EXTCON_DISP_VGA] = {
  147. .type = EXTCON_TYPE_DISP,
  148. .id = EXTCON_DISP_VGA,
  149. .name = "VGA",
  150. },
  151. [EXTCON_DISP_DP] = {
  152. .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
  153. .id = EXTCON_DISP_DP,
  154. .name = "DP",
  155. },
  156. [EXTCON_DISP_HMD] = {
  157. .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
  158. .id = EXTCON_DISP_HMD,
  159. .name = "HMD",
  160. },
  161. /* Miscellaneous external connector */
  162. [EXTCON_DOCK] = {
  163. .type = EXTCON_TYPE_MISC,
  164. .id = EXTCON_DOCK,
  165. .name = "DOCK",
  166. },
  167. [EXTCON_JIG] = {
  168. .type = EXTCON_TYPE_MISC,
  169. .id = EXTCON_JIG,
  170. .name = "JIG",
  171. },
  172. [EXTCON_MECHANICAL] = {
  173. .type = EXTCON_TYPE_MISC,
  174. .id = EXTCON_MECHANICAL,
  175. .name = "MECHANICAL",
  176. },
  177. { /* sentinel */ }
  178. };
  179. /**
  180. * struct extcon_cable - An internal data for an external connector.
  181. * @edev: the extcon device
  182. * @cable_index: the index of this cable in the edev
  183. * @attr_g: the attribute group for the cable
  184. * @attr_name: "name" sysfs entry
  185. * @attr_state: "state" sysfs entry
  186. * @attrs: the array pointing to attr_name and attr_state for attr_g
  187. */
  188. struct extcon_cable {
  189. struct extcon_dev *edev;
  190. int cable_index;
  191. struct attribute_group attr_g;
  192. struct device_attribute attr_name;
  193. struct device_attribute attr_state;
  194. struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
  195. union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
  196. union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
  197. union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
  198. union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
  199. unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
  200. unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
  201. unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
  202. unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
  203. };
  204. static struct class *extcon_class;
  205. static LIST_HEAD(extcon_dev_list);
  206. static DEFINE_MUTEX(extcon_dev_list_lock);
  207. static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
  208. {
  209. int i = 0;
  210. if (!edev->mutually_exclusive)
  211. return 0;
  212. for (i = 0; edev->mutually_exclusive[i]; i++) {
  213. int weight;
  214. u32 correspondants = new_state & edev->mutually_exclusive[i];
  215. /* calculate the total number of bits set */
  216. weight = hweight32(correspondants);
  217. if (weight > 1)
  218. return i + 1;
  219. }
  220. return 0;
  221. }
  222. static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
  223. {
  224. int i;
  225. /* Find the the index of extcon cable in edev->supported_cable */
  226. for (i = 0; i < edev->max_supported; i++) {
  227. if (edev->supported_cable[i] == id)
  228. return i;
  229. }
  230. return -EINVAL;
  231. }
  232. static int get_extcon_type(unsigned int prop)
  233. {
  234. switch (prop) {
  235. case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
  236. return EXTCON_TYPE_USB;
  237. case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
  238. return EXTCON_TYPE_CHG;
  239. case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
  240. return EXTCON_TYPE_JACK;
  241. case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
  242. return EXTCON_TYPE_DISP;
  243. default:
  244. return -EINVAL;
  245. }
  246. }
  247. static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
  248. {
  249. return !!(edev->state & BIT(index));
  250. }
  251. static bool is_extcon_changed(struct extcon_dev *edev, int index,
  252. bool new_state)
  253. {
  254. int state = !!(edev->state & BIT(index));
  255. return (state != new_state);
  256. }
  257. static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
  258. {
  259. int type;
  260. /* Check whether the property is supported or not. */
  261. type = get_extcon_type(prop);
  262. if (type < 0)
  263. return false;
  264. /* Check whether a specific extcon id supports the property or not. */
  265. return !!(extcon_info[id].type & type);
  266. }
  267. static int is_extcon_property_capability(struct extcon_dev *edev,
  268. unsigned int id, int index,unsigned int prop)
  269. {
  270. struct extcon_cable *cable;
  271. int type, ret;
  272. /* Check whether the property is supported or not. */
  273. type = get_extcon_type(prop);
  274. if (type < 0)
  275. return type;
  276. cable = &edev->cables[index];
  277. switch (type) {
  278. case EXTCON_TYPE_USB:
  279. ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
  280. break;
  281. case EXTCON_TYPE_CHG:
  282. ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
  283. break;
  284. case EXTCON_TYPE_JACK:
  285. ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
  286. break;
  287. case EXTCON_TYPE_DISP:
  288. ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
  289. break;
  290. default:
  291. ret = -EINVAL;
  292. }
  293. return ret;
  294. }
  295. static void init_property(struct extcon_dev *edev, unsigned int id, int index)
  296. {
  297. unsigned int type = extcon_info[id].type;
  298. struct extcon_cable *cable = &edev->cables[index];
  299. if (EXTCON_TYPE_USB & type)
  300. memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
  301. if (EXTCON_TYPE_CHG & type)
  302. memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
  303. if (EXTCON_TYPE_JACK & type)
  304. memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
  305. if (EXTCON_TYPE_DISP & type)
  306. memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
  307. }
  308. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  309. char *buf)
  310. {
  311. int i, count = 0;
  312. struct extcon_dev *edev = dev_get_drvdata(dev);
  313. if (edev->max_supported == 0)
  314. return sprintf(buf, "%u\n", edev->state);
  315. for (i = 0; i < edev->max_supported; i++) {
  316. count += sprintf(buf + count, "%s=%d\n",
  317. extcon_info[edev->supported_cable[i]].name,
  318. !!(edev->state & BIT(i)));
  319. }
  320. return count;
  321. }
  322. static DEVICE_ATTR_RO(state);
  323. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  324. char *buf)
  325. {
  326. struct extcon_dev *edev = dev_get_drvdata(dev);
  327. return sprintf(buf, "%s\n", edev->name);
  328. }
  329. static DEVICE_ATTR_RO(name);
  330. static ssize_t cable_name_show(struct device *dev,
  331. struct device_attribute *attr, char *buf)
  332. {
  333. struct extcon_cable *cable = container_of(attr, struct extcon_cable,
  334. attr_name);
  335. int i = cable->cable_index;
  336. return sprintf(buf, "%s\n",
  337. extcon_info[cable->edev->supported_cable[i]].name);
  338. }
  339. static ssize_t cable_state_show(struct device *dev,
  340. struct device_attribute *attr, char *buf)
  341. {
  342. struct extcon_cable *cable = container_of(attr, struct extcon_cable,
  343. attr_state);
  344. int i = cable->cable_index;
  345. return sprintf(buf, "%d\n",
  346. extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
  347. }
  348. /**
  349. * extcon_sync() - Synchronize the state for an external connector.
  350. * @edev: the extcon device
  351. *
  352. * Note that this function send a notification in order to synchronize
  353. * the state and property of an external connector.
  354. *
  355. * Returns 0 if success or error number if fail.
  356. */
  357. int extcon_sync(struct extcon_dev *edev, unsigned int id)
  358. {
  359. char name_buf[120];
  360. char state_buf[120];
  361. char *prop_buf;
  362. char *envp[3];
  363. int env_offset = 0;
  364. int length;
  365. int index;
  366. int state;
  367. unsigned long flags;
  368. if (!edev)
  369. return -EINVAL;
  370. index = find_cable_index_by_id(edev, id);
  371. if (index < 0)
  372. return index;
  373. spin_lock_irqsave(&edev->lock, flags);
  374. state = !!(edev->state & BIT(index));
  375. spin_unlock_irqrestore(&edev->lock, flags);
  376. /*
  377. * Call functions in a raw notifier chain for the specific one
  378. * external connector.
  379. */
  380. raw_notifier_call_chain(&edev->nh[index], state, edev);
  381. /*
  382. * Call functions in a raw notifier chain for the all supported
  383. * external connectors.
  384. */
  385. raw_notifier_call_chain(&edev->nh_all, state, edev);
  386. spin_lock_irqsave(&edev->lock, flags);
  387. /* This could be in interrupt handler */
  388. prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
  389. if (!prop_buf) {
  390. /* Unlock early before uevent */
  391. spin_unlock_irqrestore(&edev->lock, flags);
  392. dev_err(&edev->dev, "out of memory in extcon_set_state\n");
  393. kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
  394. return -ENOMEM;
  395. }
  396. length = name_show(&edev->dev, NULL, prop_buf);
  397. if (length > 0) {
  398. if (prop_buf[length - 1] == '\n')
  399. prop_buf[length - 1] = 0;
  400. snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
  401. envp[env_offset++] = name_buf;
  402. }
  403. length = state_show(&edev->dev, NULL, prop_buf);
  404. if (length > 0) {
  405. if (prop_buf[length - 1] == '\n')
  406. prop_buf[length - 1] = 0;
  407. snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
  408. envp[env_offset++] = state_buf;
  409. }
  410. envp[env_offset] = NULL;
  411. /* Unlock early before uevent */
  412. spin_unlock_irqrestore(&edev->lock, flags);
  413. kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
  414. free_page((unsigned long)prop_buf);
  415. return 0;
  416. }
  417. EXPORT_SYMBOL_GPL(extcon_sync);
  418. /**
  419. * extcon_get_state() - Get the state of an external connector.
  420. * @edev: the extcon device
  421. * @id: the unique id indicating an external connector
  422. *
  423. * Returns 0 if success or error number if fail.
  424. */
  425. int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
  426. {
  427. int index, state;
  428. unsigned long flags;
  429. if (!edev)
  430. return -EINVAL;
  431. index = find_cable_index_by_id(edev, id);
  432. if (index < 0)
  433. return index;
  434. spin_lock_irqsave(&edev->lock, flags);
  435. state = is_extcon_attached(edev, index);
  436. spin_unlock_irqrestore(&edev->lock, flags);
  437. return state;
  438. }
  439. EXPORT_SYMBOL_GPL(extcon_get_state);
  440. /**
  441. * extcon_set_state() - Set the state of an external connector.
  442. * @edev: the extcon device
  443. * @id: the unique id indicating an external connector
  444. * @state: the new state of an external connector.
  445. * the default semantics is true: attached / false: detached.
  446. *
  447. * Note that this function set the state of an external connector without
  448. * a notification. To synchronize the state of an external connector,
  449. * have to use extcon_set_state_sync() and extcon_sync().
  450. *
  451. * Returns 0 if success or error number if fail.
  452. */
  453. int extcon_set_state(struct extcon_dev *edev, unsigned int id, bool state)
  454. {
  455. unsigned long flags;
  456. int index, ret = 0;
  457. if (!edev)
  458. return -EINVAL;
  459. index = find_cable_index_by_id(edev, id);
  460. if (index < 0)
  461. return index;
  462. spin_lock_irqsave(&edev->lock, flags);
  463. /* Check whether the external connector's state is changed. */
  464. if (!is_extcon_changed(edev, index, state))
  465. goto out;
  466. if (check_mutually_exclusive(edev,
  467. (edev->state & ~BIT(index)) | (state & BIT(index)))) {
  468. ret = -EPERM;
  469. goto out;
  470. }
  471. /*
  472. * Initialize the value of extcon property before setting
  473. * the detached state for an external connector.
  474. */
  475. if (!state)
  476. init_property(edev, id, index);
  477. /* Update the state for an external connector. */
  478. if (state)
  479. edev->state |= BIT(index);
  480. else
  481. edev->state &= ~(BIT(index));
  482. out:
  483. spin_unlock_irqrestore(&edev->lock, flags);
  484. return ret;
  485. }
  486. EXPORT_SYMBOL_GPL(extcon_set_state);
  487. /**
  488. * extcon_set_state_sync() - Set the state of an external connector with sync.
  489. * @edev: the extcon device
  490. * @id: the unique id indicating an external connector
  491. * @state: the new state of external connector.
  492. * the default semantics is true: attached / false: detached.
  493. *
  494. * Note that this function set the state of external connector
  495. * and synchronize the state by sending a notification.
  496. *
  497. * Returns 0 if success or error number if fail.
  498. */
  499. int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id, bool state)
  500. {
  501. int ret, index;
  502. unsigned long flags;
  503. index = find_cable_index_by_id(edev, id);
  504. if (index < 0)
  505. return index;
  506. /* Check whether the external connector's state is changed. */
  507. spin_lock_irqsave(&edev->lock, flags);
  508. ret = is_extcon_changed(edev, index, state);
  509. spin_unlock_irqrestore(&edev->lock, flags);
  510. if (!ret)
  511. return 0;
  512. ret = extcon_set_state(edev, id, state);
  513. if (ret < 0)
  514. return ret;
  515. return extcon_sync(edev, id);
  516. }
  517. EXPORT_SYMBOL_GPL(extcon_set_state_sync);
  518. /**
  519. * extcon_get_property() - Get the property value of an external connector.
  520. * @edev: the extcon device
  521. * @id: the unique id indicating an external connector
  522. * @prop: the property id indicating an extcon property
  523. * @prop_val: the pointer which store the value of extcon property
  524. *
  525. * Note that when getting the property value of external connector,
  526. * the external connector should be attached. If detached state, function
  527. * return 0 without property value. Also, the each property should be
  528. * included in the list of supported properties according to extcon type.
  529. *
  530. * Returns 0 if success or error number if fail.
  531. */
  532. int extcon_get_property(struct extcon_dev *edev, unsigned int id,
  533. unsigned int prop,
  534. union extcon_property_value *prop_val)
  535. {
  536. struct extcon_cable *cable;
  537. unsigned long flags;
  538. int index, ret = 0;
  539. *prop_val = (union extcon_property_value){0};
  540. if (!edev)
  541. return -EINVAL;
  542. /* Check whether the property is supported or not */
  543. if (!is_extcon_property_supported(id, prop))
  544. return -EINVAL;
  545. /* Find the cable index of external connector by using id */
  546. index = find_cable_index_by_id(edev, id);
  547. if (index < 0)
  548. return index;
  549. spin_lock_irqsave(&edev->lock, flags);
  550. /* Check whether the property is available or not. */
  551. if (!is_extcon_property_capability(edev, id, index, prop)) {
  552. spin_unlock_irqrestore(&edev->lock, flags);
  553. return -EPERM;
  554. }
  555. /*
  556. * Check whether the external connector is attached.
  557. * If external connector is detached, the user can not
  558. * get the property value.
  559. */
  560. if (!is_extcon_attached(edev, index)) {
  561. spin_unlock_irqrestore(&edev->lock, flags);
  562. return 0;
  563. }
  564. cable = &edev->cables[index];
  565. /* Get the property value according to extcon type */
  566. switch (prop) {
  567. case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
  568. *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
  569. break;
  570. case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
  571. *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
  572. break;
  573. case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
  574. *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
  575. break;
  576. case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
  577. *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
  578. break;
  579. default:
  580. ret = -EINVAL;
  581. break;
  582. }
  583. spin_unlock_irqrestore(&edev->lock, flags);
  584. return ret;
  585. }
  586. EXPORT_SYMBOL_GPL(extcon_get_property);
  587. /**
  588. * extcon_set_property() - Set the property value of an external connector.
  589. * @edev: the extcon device
  590. * @id: the unique id indicating an external connector
  591. * @prop: the property id indicating an extcon property
  592. * @prop_val: the pointer including the new value of extcon property
  593. *
  594. * Note that each property should be included in the list of supported
  595. * properties according to the extcon type.
  596. *
  597. * Returns 0 if success or error number if fail.
  598. */
  599. int extcon_set_property(struct extcon_dev *edev, unsigned int id,
  600. unsigned int prop,
  601. union extcon_property_value prop_val)
  602. {
  603. struct extcon_cable *cable;
  604. unsigned long flags;
  605. int index, ret = 0;
  606. if (!edev)
  607. return -EINVAL;
  608. /* Check whether the property is supported or not */
  609. if (!is_extcon_property_supported(id, prop))
  610. return -EINVAL;
  611. /* Find the cable index of external connector by using id */
  612. index = find_cable_index_by_id(edev, id);
  613. if (index < 0)
  614. return index;
  615. spin_lock_irqsave(&edev->lock, flags);
  616. /* Check whether the property is available or not. */
  617. if (!is_extcon_property_capability(edev, id, index, prop)) {
  618. spin_unlock_irqrestore(&edev->lock, flags);
  619. return -EPERM;
  620. }
  621. cable = &edev->cables[index];
  622. /* Set the property value according to extcon type */
  623. switch (prop) {
  624. case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
  625. cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
  626. break;
  627. case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
  628. cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
  629. break;
  630. case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
  631. cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
  632. break;
  633. case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
  634. cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
  635. break;
  636. default:
  637. ret = -EINVAL;
  638. break;
  639. }
  640. spin_unlock_irqrestore(&edev->lock, flags);
  641. return ret;
  642. }
  643. EXPORT_SYMBOL_GPL(extcon_set_property);
  644. /**
  645. * extcon_set_property_sync() - Set property of an external connector with sync.
  646. * @prop_val: the pointer including the new value of extcon property
  647. *
  648. * Note that when setting the property value of external connector,
  649. * the external connector should be attached. The each property should
  650. * be included in the list of supported properties according to extcon type.
  651. *
  652. * Returns 0 if success or error number if fail.
  653. */
  654. int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
  655. unsigned int prop,
  656. union extcon_property_value prop_val)
  657. {
  658. int ret;
  659. ret = extcon_set_property(edev, id, prop, prop_val);
  660. if (ret < 0)
  661. return ret;
  662. return extcon_sync(edev, id);
  663. }
  664. EXPORT_SYMBOL_GPL(extcon_set_property_sync);
  665. /**
  666. * extcon_get_property_capability() - Get the capability of the property
  667. * for an external connector.
  668. * @edev: the extcon device
  669. * @id: the unique id indicating an external connector
  670. * @prop: the property id indicating an extcon property
  671. *
  672. * Returns 1 if the property is available or 0 if not available.
  673. */
  674. int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
  675. unsigned int prop)
  676. {
  677. int index;
  678. if (!edev)
  679. return -EINVAL;
  680. /* Check whether the property is supported or not */
  681. if (!is_extcon_property_supported(id, prop))
  682. return -EINVAL;
  683. /* Find the cable index of external connector by using id */
  684. index = find_cable_index_by_id(edev, id);
  685. if (index < 0)
  686. return index;
  687. return is_extcon_property_capability(edev, id, index, prop);
  688. }
  689. EXPORT_SYMBOL_GPL(extcon_get_property_capability);
  690. /**
  691. * extcon_set_property_capability() - Set the capability of the property
  692. * for an external connector.
  693. * @edev: the extcon device
  694. * @id: the unique id indicating an external connector
  695. * @prop: the property id indicating an extcon property
  696. *
  697. * Note that this function set the capability of the property
  698. * for an external connector in order to mark the bit in capability
  699. * bitmap which mean the available state of the property.
  700. *
  701. * Returns 0 if success or error number if fail.
  702. */
  703. int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
  704. unsigned int prop)
  705. {
  706. struct extcon_cable *cable;
  707. int index, type, ret = 0;
  708. if (!edev)
  709. return -EINVAL;
  710. /* Check whether the property is supported or not. */
  711. if (!is_extcon_property_supported(id, prop))
  712. return -EINVAL;
  713. /* Find the cable index of external connector by using id. */
  714. index = find_cable_index_by_id(edev, id);
  715. if (index < 0)
  716. return index;
  717. type = get_extcon_type(prop);
  718. if (type < 0)
  719. return type;
  720. cable = &edev->cables[index];
  721. switch (type) {
  722. case EXTCON_TYPE_USB:
  723. __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
  724. break;
  725. case EXTCON_TYPE_CHG:
  726. __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
  727. break;
  728. case EXTCON_TYPE_JACK:
  729. __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
  730. break;
  731. case EXTCON_TYPE_DISP:
  732. __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
  733. break;
  734. default:
  735. ret = -EINVAL;
  736. }
  737. return ret;
  738. }
  739. EXPORT_SYMBOL_GPL(extcon_set_property_capability);
  740. /**
  741. * extcon_get_extcon_dev() - Get the extcon device instance from the name.
  742. * @extcon_name: the extcon name provided with extcon_dev_register()
  743. *
  744. * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
  745. */
  746. struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
  747. {
  748. struct extcon_dev *sd;
  749. if (!extcon_name)
  750. return ERR_PTR(-EINVAL);
  751. mutex_lock(&extcon_dev_list_lock);
  752. list_for_each_entry(sd, &extcon_dev_list, entry) {
  753. if (!strcmp(sd->name, extcon_name))
  754. goto out;
  755. }
  756. sd = NULL;
  757. out:
  758. mutex_unlock(&extcon_dev_list_lock);
  759. return sd;
  760. }
  761. EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
  762. /**
  763. * extcon_register_notifier() - Register a notifier block to get notified by
  764. * any state changes from the extcon.
  765. * @edev: the extcon device
  766. * @id: the unique id indicating an external connector
  767. * @nb: a notifier block to be registered
  768. *
  769. * Note that the second parameter given to the callback of nb (val) is
  770. * the current state of an external connector and the third pameter
  771. * is the pointer of extcon device.
  772. *
  773. * Returns 0 if success or error number if fail.
  774. */
  775. int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
  776. struct notifier_block *nb)
  777. {
  778. unsigned long flags;
  779. int ret, idx;
  780. if (!edev || !nb)
  781. return -EINVAL;
  782. idx = find_cable_index_by_id(edev, id);
  783. if (idx < 0)
  784. return idx;
  785. spin_lock_irqsave(&edev->lock, flags);
  786. ret = raw_notifier_chain_register(&edev->nh[idx], nb);
  787. spin_unlock_irqrestore(&edev->lock, flags);
  788. return ret;
  789. }
  790. EXPORT_SYMBOL_GPL(extcon_register_notifier);
  791. /**
  792. * extcon_unregister_notifier() - Unregister a notifier block from the extcon.
  793. * @edev: the extcon device
  794. * @id: the unique id indicating an external connector
  795. * @nb: a notifier block to be registered
  796. *
  797. * Returns 0 if success or error number if fail.
  798. */
  799. int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
  800. struct notifier_block *nb)
  801. {
  802. unsigned long flags;
  803. int ret, idx;
  804. if (!edev || !nb)
  805. return -EINVAL;
  806. idx = find_cable_index_by_id(edev, id);
  807. if (idx < 0)
  808. return idx;
  809. spin_lock_irqsave(&edev->lock, flags);
  810. ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
  811. spin_unlock_irqrestore(&edev->lock, flags);
  812. return ret;
  813. }
  814. EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
  815. /**
  816. * extcon_register_notifier_all() - Register a notifier block for all connectors.
  817. * @edev: the extcon device
  818. * @nb: a notifier block to be registered
  819. *
  820. * Note that this function registers a notifier block in order to receive
  821. * the state change of all supported external connectors from extcon device.
  822. * And the second parameter given to the callback of nb (val) is
  823. * the current state and the third pameter is the pointer of extcon device.
  824. *
  825. * Returns 0 if success or error number if fail.
  826. */
  827. int extcon_register_notifier_all(struct extcon_dev *edev,
  828. struct notifier_block *nb)
  829. {
  830. unsigned long flags;
  831. int ret;
  832. if (!edev || !nb)
  833. return -EINVAL;
  834. spin_lock_irqsave(&edev->lock, flags);
  835. ret = raw_notifier_chain_register(&edev->nh_all, nb);
  836. spin_unlock_irqrestore(&edev->lock, flags);
  837. return ret;
  838. }
  839. EXPORT_SYMBOL_GPL(extcon_register_notifier_all);
  840. /**
  841. * extcon_unregister_notifier_all() - Unregister a notifier block from extcon.
  842. * @edev: the extcon device
  843. * @nb: a notifier block to be registered
  844. *
  845. * Returns 0 if success or error number if fail.
  846. */
  847. int extcon_unregister_notifier_all(struct extcon_dev *edev,
  848. struct notifier_block *nb)
  849. {
  850. unsigned long flags;
  851. int ret;
  852. if (!edev || !nb)
  853. return -EINVAL;
  854. spin_lock_irqsave(&edev->lock, flags);
  855. ret = raw_notifier_chain_unregister(&edev->nh_all, nb);
  856. spin_unlock_irqrestore(&edev->lock, flags);
  857. return ret;
  858. }
  859. EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all);
  860. static struct attribute *extcon_attrs[] = {
  861. &dev_attr_state.attr,
  862. &dev_attr_name.attr,
  863. NULL,
  864. };
  865. ATTRIBUTE_GROUPS(extcon);
  866. static int create_extcon_class(void)
  867. {
  868. if (!extcon_class) {
  869. extcon_class = class_create(THIS_MODULE, "extcon");
  870. if (IS_ERR(extcon_class))
  871. return PTR_ERR(extcon_class);
  872. extcon_class->dev_groups = extcon_groups;
  873. }
  874. return 0;
  875. }
  876. static void extcon_dev_release(struct device *dev)
  877. {
  878. }
  879. static const char *muex_name = "mutually_exclusive";
  880. static void dummy_sysfs_dev_release(struct device *dev)
  881. {
  882. }
  883. /*
  884. * extcon_dev_allocate() - Allocate the memory of extcon device.
  885. * @supported_cable: the array of the supported external connectors
  886. * ending with EXTCON_NONE.
  887. *
  888. * Note that this function allocates the memory for extcon device
  889. * and initialize default setting for the extcon device.
  890. *
  891. * Returns the pointer memory of allocated extcon_dev if success
  892. * or ERR_PTR(err) if fail.
  893. */
  894. struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
  895. {
  896. struct extcon_dev *edev;
  897. if (!supported_cable)
  898. return ERR_PTR(-EINVAL);
  899. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  900. if (!edev)
  901. return ERR_PTR(-ENOMEM);
  902. edev->max_supported = 0;
  903. edev->supported_cable = supported_cable;
  904. return edev;
  905. }
  906. /*
  907. * extcon_dev_free() - Free the memory of extcon device.
  908. * @edev: the extcon device
  909. */
  910. void extcon_dev_free(struct extcon_dev *edev)
  911. {
  912. kfree(edev);
  913. }
  914. EXPORT_SYMBOL_GPL(extcon_dev_free);
  915. /**
  916. * extcon_dev_register() - Register an new extcon device
  917. * @edev: the extcon device to be registered
  918. *
  919. * Among the members of edev struct, please set the "user initializing data"
  920. * do not set the values of "internal data", which are initialized by
  921. * this function.
  922. *
  923. * Note that before calling this funciton, have to allocate the memory
  924. * of an extcon device by using the extcon_dev_allocate(). And the extcon
  925. * dev should include the supported_cable information.
  926. *
  927. * Returns 0 if success or error number if fail.
  928. */
  929. int extcon_dev_register(struct extcon_dev *edev)
  930. {
  931. int ret, index = 0;
  932. static atomic_t edev_no = ATOMIC_INIT(-1);
  933. if (!extcon_class) {
  934. ret = create_extcon_class();
  935. if (ret < 0)
  936. return ret;
  937. }
  938. if (!edev || !edev->supported_cable)
  939. return -EINVAL;
  940. for (; edev->supported_cable[index] != EXTCON_NONE; index++);
  941. edev->max_supported = index;
  942. if (index > SUPPORTED_CABLE_MAX) {
  943. dev_err(&edev->dev,
  944. "exceed the maximum number of supported cables\n");
  945. return -EINVAL;
  946. }
  947. edev->dev.class = extcon_class;
  948. edev->dev.release = extcon_dev_release;
  949. edev->name = dev_name(edev->dev.parent);
  950. if (IS_ERR_OR_NULL(edev->name)) {
  951. dev_err(&edev->dev,
  952. "extcon device name is null\n");
  953. return -EINVAL;
  954. }
  955. dev_set_name(&edev->dev, "extcon%lu",
  956. (unsigned long)atomic_inc_return(&edev_no));
  957. if (edev->max_supported) {
  958. char *str;
  959. struct extcon_cable *cable;
  960. edev->cables = kcalloc(edev->max_supported,
  961. sizeof(struct extcon_cable),
  962. GFP_KERNEL);
  963. if (!edev->cables) {
  964. ret = -ENOMEM;
  965. goto err_sysfs_alloc;
  966. }
  967. for (index = 0; index < edev->max_supported; index++) {
  968. cable = &edev->cables[index];
  969. str = kasprintf(GFP_KERNEL, "cable.%d", index);
  970. if (!str) {
  971. for (index--; index >= 0; index--) {
  972. cable = &edev->cables[index];
  973. kfree(cable->attr_g.name);
  974. }
  975. ret = -ENOMEM;
  976. goto err_alloc_cables;
  977. }
  978. cable->edev = edev;
  979. cable->cable_index = index;
  980. cable->attrs[0] = &cable->attr_name.attr;
  981. cable->attrs[1] = &cable->attr_state.attr;
  982. cable->attrs[2] = NULL;
  983. cable->attr_g.name = str;
  984. cable->attr_g.attrs = cable->attrs;
  985. sysfs_attr_init(&cable->attr_name.attr);
  986. cable->attr_name.attr.name = "name";
  987. cable->attr_name.attr.mode = 0444;
  988. cable->attr_name.show = cable_name_show;
  989. sysfs_attr_init(&cable->attr_state.attr);
  990. cable->attr_state.attr.name = "state";
  991. cable->attr_state.attr.mode = 0444;
  992. cable->attr_state.show = cable_state_show;
  993. }
  994. }
  995. if (edev->max_supported && edev->mutually_exclusive) {
  996. char *name;
  997. /* Count the size of mutually_exclusive array */
  998. for (index = 0; edev->mutually_exclusive[index]; index++)
  999. ;
  1000. edev->attrs_muex = kcalloc(index + 1,
  1001. sizeof(struct attribute *),
  1002. GFP_KERNEL);
  1003. if (!edev->attrs_muex) {
  1004. ret = -ENOMEM;
  1005. goto err_muex;
  1006. }
  1007. edev->d_attrs_muex = kcalloc(index,
  1008. sizeof(struct device_attribute),
  1009. GFP_KERNEL);
  1010. if (!edev->d_attrs_muex) {
  1011. ret = -ENOMEM;
  1012. kfree(edev->attrs_muex);
  1013. goto err_muex;
  1014. }
  1015. for (index = 0; edev->mutually_exclusive[index]; index++) {
  1016. name = kasprintf(GFP_KERNEL, "0x%x",
  1017. edev->mutually_exclusive[index]);
  1018. if (!name) {
  1019. for (index--; index >= 0; index--) {
  1020. kfree(edev->d_attrs_muex[index].attr.
  1021. name);
  1022. }
  1023. kfree(edev->d_attrs_muex);
  1024. kfree(edev->attrs_muex);
  1025. ret = -ENOMEM;
  1026. goto err_muex;
  1027. }
  1028. sysfs_attr_init(&edev->d_attrs_muex[index].attr);
  1029. edev->d_attrs_muex[index].attr.name = name;
  1030. edev->d_attrs_muex[index].attr.mode = 0000;
  1031. edev->attrs_muex[index] = &edev->d_attrs_muex[index]
  1032. .attr;
  1033. }
  1034. edev->attr_g_muex.name = muex_name;
  1035. edev->attr_g_muex.attrs = edev->attrs_muex;
  1036. }
  1037. if (edev->max_supported) {
  1038. edev->extcon_dev_type.groups =
  1039. kcalloc(edev->max_supported + 2,
  1040. sizeof(struct attribute_group *),
  1041. GFP_KERNEL);
  1042. if (!edev->extcon_dev_type.groups) {
  1043. ret = -ENOMEM;
  1044. goto err_alloc_groups;
  1045. }
  1046. edev->extcon_dev_type.name = dev_name(&edev->dev);
  1047. edev->extcon_dev_type.release = dummy_sysfs_dev_release;
  1048. for (index = 0; index < edev->max_supported; index++)
  1049. edev->extcon_dev_type.groups[index] =
  1050. &edev->cables[index].attr_g;
  1051. if (edev->mutually_exclusive)
  1052. edev->extcon_dev_type.groups[index] =
  1053. &edev->attr_g_muex;
  1054. edev->dev.type = &edev->extcon_dev_type;
  1055. }
  1056. ret = device_register(&edev->dev);
  1057. if (ret) {
  1058. put_device(&edev->dev);
  1059. goto err_dev;
  1060. }
  1061. spin_lock_init(&edev->lock);
  1062. edev->nh = devm_kcalloc(&edev->dev, edev->max_supported,
  1063. sizeof(*edev->nh), GFP_KERNEL);
  1064. if (!edev->nh) {
  1065. ret = -ENOMEM;
  1066. device_unregister(&edev->dev);
  1067. goto err_dev;
  1068. }
  1069. for (index = 0; index < edev->max_supported; index++)
  1070. RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
  1071. RAW_INIT_NOTIFIER_HEAD(&edev->nh_all);
  1072. dev_set_drvdata(&edev->dev, edev);
  1073. edev->state = 0;
  1074. mutex_lock(&extcon_dev_list_lock);
  1075. list_add(&edev->entry, &extcon_dev_list);
  1076. mutex_unlock(&extcon_dev_list_lock);
  1077. return 0;
  1078. err_dev:
  1079. if (edev->max_supported)
  1080. kfree(edev->extcon_dev_type.groups);
  1081. err_alloc_groups:
  1082. if (edev->max_supported && edev->mutually_exclusive) {
  1083. for (index = 0; edev->mutually_exclusive[index]; index++)
  1084. kfree(edev->d_attrs_muex[index].attr.name);
  1085. kfree(edev->d_attrs_muex);
  1086. kfree(edev->attrs_muex);
  1087. }
  1088. err_muex:
  1089. for (index = 0; index < edev->max_supported; index++)
  1090. kfree(edev->cables[index].attr_g.name);
  1091. err_alloc_cables:
  1092. if (edev->max_supported)
  1093. kfree(edev->cables);
  1094. err_sysfs_alloc:
  1095. return ret;
  1096. }
  1097. EXPORT_SYMBOL_GPL(extcon_dev_register);
  1098. /**
  1099. * extcon_dev_unregister() - Unregister the extcon device.
  1100. * @edev: the extcon device to be unregistered.
  1101. *
  1102. * Note that this does not call kfree(edev) because edev was not allocated
  1103. * by this class.
  1104. */
  1105. void extcon_dev_unregister(struct extcon_dev *edev)
  1106. {
  1107. int index;
  1108. if (!edev)
  1109. return;
  1110. mutex_lock(&extcon_dev_list_lock);
  1111. list_del(&edev->entry);
  1112. mutex_unlock(&extcon_dev_list_lock);
  1113. if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
  1114. dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
  1115. dev_name(&edev->dev));
  1116. return;
  1117. }
  1118. device_unregister(&edev->dev);
  1119. if (edev->mutually_exclusive && edev->max_supported) {
  1120. for (index = 0; edev->mutually_exclusive[index];
  1121. index++)
  1122. kfree(edev->d_attrs_muex[index].attr.name);
  1123. kfree(edev->d_attrs_muex);
  1124. kfree(edev->attrs_muex);
  1125. }
  1126. for (index = 0; index < edev->max_supported; index++)
  1127. kfree(edev->cables[index].attr_g.name);
  1128. if (edev->max_supported) {
  1129. kfree(edev->extcon_dev_type.groups);
  1130. kfree(edev->cables);
  1131. }
  1132. put_device(&edev->dev);
  1133. }
  1134. EXPORT_SYMBOL_GPL(extcon_dev_unregister);
  1135. #ifdef CONFIG_OF
  1136. /*
  1137. * extcon_find_edev_by_node - Find the extcon device from devicetree.
  1138. * @node : OF node identifying edev
  1139. *
  1140. * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
  1141. */
  1142. struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
  1143. {
  1144. struct extcon_dev *edev;
  1145. mutex_lock(&extcon_dev_list_lock);
  1146. list_for_each_entry(edev, &extcon_dev_list, entry)
  1147. if (edev->dev.parent && edev->dev.parent->of_node == node)
  1148. goto out;
  1149. edev = ERR_PTR(-EPROBE_DEFER);
  1150. out:
  1151. mutex_unlock(&extcon_dev_list_lock);
  1152. return edev;
  1153. }
  1154. /*
  1155. * extcon_get_edev_by_phandle - Get the extcon device from devicetree.
  1156. * @dev : the instance to the given device
  1157. * @index : the index into list of extcon_dev
  1158. *
  1159. * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
  1160. */
  1161. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  1162. {
  1163. struct device_node *node;
  1164. struct extcon_dev *edev;
  1165. if (!dev)
  1166. return ERR_PTR(-EINVAL);
  1167. if (!dev->of_node) {
  1168. dev_dbg(dev, "device does not have a device node entry\n");
  1169. return ERR_PTR(-EINVAL);
  1170. }
  1171. node = of_parse_phandle(dev->of_node, "extcon", index);
  1172. if (!node) {
  1173. dev_dbg(dev, "failed to get phandle in %pOF node\n",
  1174. dev->of_node);
  1175. return ERR_PTR(-ENODEV);
  1176. }
  1177. edev = extcon_find_edev_by_node(node);
  1178. of_node_put(node);
  1179. return edev;
  1180. }
  1181. #else
  1182. struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
  1183. {
  1184. return ERR_PTR(-ENOSYS);
  1185. }
  1186. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  1187. {
  1188. return ERR_PTR(-ENOSYS);
  1189. }
  1190. #endif /* CONFIG_OF */
  1191. EXPORT_SYMBOL_GPL(extcon_find_edev_by_node);
  1192. EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
  1193. /**
  1194. * extcon_get_edev_name() - Get the name of the extcon device.
  1195. * @edev: the extcon device
  1196. */
  1197. const char *extcon_get_edev_name(struct extcon_dev *edev)
  1198. {
  1199. return !edev ? NULL : edev->name;
  1200. }
  1201. EXPORT_SYMBOL_GPL(extcon_get_edev_name);
  1202. static int __init extcon_class_init(void)
  1203. {
  1204. return create_extcon_class();
  1205. }
  1206. module_init(extcon_class_init);
  1207. static void __exit extcon_class_exit(void)
  1208. {
  1209. class_destroy(extcon_class);
  1210. }
  1211. module_exit(extcon_class_exit);
  1212. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  1213. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  1214. MODULE_DESCRIPTION("External Connector (extcon) framework");
  1215. MODULE_LICENSE("GPL v2");